I have a Result class in which I calculate many dataframes (around 30) and assign them to attributes of the class.
class Result:
def __init__(self, df1)
self.df1=df1
I want to write the dataframes to excel at specified positions. For this I have a function
append_df_to_excel(df, sheet_name='Sheet1', startrow=0, startcol=0)
which takes as parameters a dataframe, sheet_name and startrow and startcolumn.
What I am doing for now is I have a dictionary in the function main and I iterate thourgh it to write dataframes at specified places in the excel:
if __name__ == "__main__":
result=Result(df)
dic_to_excel = {5:result.df1}
for start_col, df in dic_to_excel.items():
append_df_to_excel(df, sheet_name="CORE", startrow=2, startcol=start_col)
I am wondering if it is a right place to use a dataclass instead of this dictionary dic_to_excel
in which i give poition of a dataframe in excel. In the dataclass I thought I could have a dataframe, sheet_name , startrow and startcol as attributes. Can I use dataclasses for this?
Sure:
from dataclasses import dataclass
@dataclass
class Result:
df: pd.DataFrame
start_row: int
start_col: int
sheet_name: str
if __name__ == "__main__":
results = [
Result(df, sheet_name="CORE", start_row=2, start_col=5),
# and so on
]
for result in results:
append_df_to_excel(result.df, sheet_name=result.sheet_name, startrow=result.start_row, startcol=result.start_col)