0

Is it possible to extract pandas dataframe from a styleframe object?

sf = StyleFrame.read_excel("my.xlsx", read_style=True)
df = sf.to_dataframe()??

Panda's read_excel() does not seem to read style from excel so I am thinking of using StyleFrame (which I just found out about), but I need underlying dataframe object so that I can do pure pandas operations on it and use it with other pure dataframes.

Also is it possible to export styleframe object's style to pandas dataframe.style?

Jorge
  • 83
  • 10

1 Answers1

2

The underlying dataframe is accessable through the data_df attribute. Keep in mind that each "cell" will contain a StyleFrame.Container object (which wraps the value and the style) but it should behave as expected.

sf = StyleFrame({'a': [1, 2]})
print(type(sf.data_df))
print(type(sf.data_df['a']))
print(type(sf.data_df['a'][0]))
print(sf.data_df['a'].mean())

Outputs

<class 'pandas.core.frame.DataFrame'>
<class 'pandas.core.series.Series'>
<class 'StyleFrame.container.Container'>
1.5

is it possible to export styleframe object's style to pandas dataframe.style?

Not at the moment, maybe in the future.

DeepSpace
  • 78,697
  • 11
  • 109
  • 154
  • sf.data_df is what I was looking for. It worked for me. I didn't find any mention of data_df in the API docs. May be it can be added there at some point. – Jorge Jul 07 '19 at 18:58
  • @Jorge glad I could help. It is not mentioned on purpose as it is generally preferred to first do all the heavy lifting on a dataframe and only then convert to a styleframe object (rather than directly messing with sf.data_df), however I can see the use-case of only being able to read an existing, styled Excel sheet. I'll consider mentioning it on the docs with a note or something – DeepSpace Jul 07 '19 at 19:13
  • Thanks @DeepSpace. To elaborate on my use case, using Excel I first create a template xls where I keep the header and 1 sample data row with my desired style. Creating style directly in Excel is very easy and intuitive. I save this 'template' xls file for subsequent use. Later, my python program generates a pandas dataframe from some other sources, converts it into df with header identical to that in 'template.xlsx' and then reads the 'template' xls to obtain each column's style and combines it with the data while saving into a new xls. – Jorge Jul 07 '19 at 19:46
  • Earlier I was doing it by creating/copying openpyxl's Cell and 'meta' objects, but now that I found StyleFrame, it is much easier to code the style part. The sf.data_df and each cell's Container object, which packaged value and style in each cell, was easy to work with. I didn't even need to export styles out sf object. – Jorge Jul 07 '19 at 19:46