Appending styleframes?
Closed this issue · 4 comments
First of all, thank you for the package --- it is quite a nice one. I have pasted the version info at the end of the message.
I have a script that builds a dataframe (to be saved in an Excel file) piece by piece. but when I try to do:
outsf = outsf.append(this)
where both outsf
and this
are styleframes initialized with a dataframe I have the following error:
KeyError Traceback (most recent call last)
/usr/local/lib/python3.8/dist-packages/styleframe/style_frame.py in __getattr__(self, attr)
109 try:
--> 110 return self._known_attrs[attr]
111 except KeyError:
KeyError: 'append'
Is it not possible to append two stylized dataframe or it's just a user (mine) error? If appending should work, I will try and post a minimal example...
Thanks!
Collecting styleframe
Downloading styleframe-4.0.0-py3-none-any.whl (32 kB)
Requirement already satisfied: jsonschema in /usr/lib/python3/dist-packages (from styleframe) (3.2.0)
Collecting colour<0.2,>=0.1.5
Downloading colour-0.1.5-py2.py3-none-any.whl (23 kB)
Requirement already satisfied: openpyxl<4,>=2.5 in /usr/lib/python3/dist-packages (from styleframe) (3.0.3)
Requirement already satisfied: pandas<2 in /usr/lib/python3/dist-packages (from styleframe) (0.25.3)
Requirement already satisfied: xlrd<1.3.0,>=1.0.0 in /usr/lib/python3/dist-packages (from styleframe) (1.1.0)
Installing collected packages: colour, styleframe
Successfully installed colour-0.1.5 styleframe-4.0.0
- That is not the full error you received, right? There should be an
AttributeError: 'StyleFrame' object has no attribute 'append'
somewhere in the traceback. - Using
.append
directly on aStyleFrame
is not supported by design (good or bad, I'm not sure anymore :) ). - However, there is a simple workaround, but it will force you to use unique indexes (in other words, using
ignore_index=True)
:
a = StyleFrame({'a': [1, 2]}, styler_obj=Styler(bg_color='yellow'))
b = StyleFrame({'a': [3, 4]}, styler_obj=Styler(bg_color='blue'))
cc = a.data_df.append(b.data_df, ignore_index=True) # must use ignore_index=True
a = StyleFrame(cc)
;-) thanks --- yes, the error was more complex, but I wished to check before if it was supposed to work.
I am solving the problem in another way now, but I think there is something I didn't get here (another user error). Basically, I was trying to style very specific cells (on every dataframe, then trying to append them). But even with one, my result is somehow unexpected (by me!). For example:
import pandas as pd
from styleframe impsa.apply_style_by_indexes(indexes_to_style=sa.iloc[0,0], styler_obj=red, overwrite_default_style=False)ort StyleFrame, Styler, utils
a = pd.DataFrame({'a': [1, 2], 'b': [1.1, 2.1]})
sa = StyleFrame(a)
red = Styler(bg_color=utils.colors.red)
sa.apply_style_by_indexes(indexes_to_style=sa.iloc[0,0], styler_obj=red, overwrite_default_style=False)
sa.to_excel("sa.xlsx").save()
when I meant to style just the top-left cell. If I try
sa.apply_style_by_indexes(indexes_to_style=sa.iloc[1,1], styler_obj=red, overwrite_default_style=False)
for the bottom-right cell, I have a KeyError
...
sa.iloc[0,0]
is the cell that contains the number 1 (A2
in the worksheet), but as the name suggests, indexes_to_style
expects an index on the dataframe level.
If you want to only style the A2
cell in the worksheet, you should be using indexes_to_style=[0]
(or, in this specific case, indexes_to_style=sa[sa['a'] == 1]
as it was originally designed to be used) along with cols_to_style='a'
:
import pandas as pd
from styleframe import StyleFrame, Styler, utils
a = pd.DataFrame({'a': [1, 2], 'b': [1.1, 2.1]})
sa = StyleFrame(a)
red = Styler(bg_color=utils.colors.red)
sa.apply_style_by_indexes(indexes_to_style=[0], cols_to_style='a', styler_obj=red, overwrite_default_style=False)
sa.to_excel("sa.xlsx").save()
Ah, thanks, I understand my error now. Let me post my final example (that works) --- maybe it can help somebody else!
import pandas as pd
from styleframe import StyleFrame, Styler, utils
a = pd.DataFrame({'a': [1, 2], 'b': [1.1, 2.1]})
sa = StyleFrame(a)
red = Styler(bg_color=utils.colors.red)
b = pd.DataFrame({'a': [3, 4], 'b': [3.1, 4.1]})
sb = StyleFrame(b)
sa.apply_style_by_indexes(indexes_to_style=[0], cols_to_style='a', styler_obj=red, overwrite_default_style=False)
sb.apply_style_by_indexes(indexes_to_style=[1], cols_to_style='b', styler_obj=red, overwrite_default_style=False)
sa.to_excel("sa.xlsx").save()
sb.to_excel("sb.xlsx").save()
aplusb = sa.data_df.append(sb.data_df, ignore_index=True)
sa = StyleFrame(aplusb)
sa.to_excel("sa_plus_b.xlsx").save()
which results in
I am closing this now, thank you for your support!