Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Saving a dictionary into an .XLSX

Using Python + Pandas, is there a quick and easy way to save a Dict (consisting of file names for the keys and several columns of data for the values) into an .XLSX file?

After some research, I tried converting it into a Pandas DataFrame (because I know you can write to an .XLSX file from a Pandas DataFrame) using the following code:

import pandas as pd
import glob
f_list = glob.glob("C:\\Users\\me\\dt\\xx\\*.xlsx")

sheets = {f: pd.read_excel(f) for f in f_list}
new_df = pd.DataFrame.from_dict(sheets, orient = "index")
new_df.to_excel("ALL_RUCDR_DATA.xlsx")

but I get this error:

TypeError: Unsupported type (class 'pandas.core.frame.DataFrame') in write().

I know it will create the dictionary successfully, and it seems to create the DataFrame, but why won't it create the file?

like image 447
chhillout Avatar asked Dec 06 '25 03:12

chhillout


1 Answers

The docs suggest you do this with the following:

with pd.ExcelWriter('path_to_file.xlsx') as writer:
    for (sheet_name, df) in sheets.items():
        df.to_excel(writer, sheet_name=sheet_name)

The error occurs as from_dict on a dict of DataFrames creates a strange DataFrame where each element is a DataFrame:

In [11]: sheets = {"a": pd.DataFrame([[1]], columns=["A"]), "b": pd.DataFrame([[2], [3]], columns=["B"])}

In [12]: pd.DataFrame.from_dict(sheets, orient='index')
Out[12]:
                0
b     B
0  2
1  3
a          A
0  1

In [13]: pd.DataFrame.from_dict(sheets, orient='index').applymap(type)
Out[13]:
                                       0
b  <class 'pandas.core.frame.DataFrame'>
a  <class 'pandas.core.frame.DataFrame'>

This isn't going to map to a excel sheet as that expects single values (e.g. int/float/string) as the elements.

If you want to merge/concat/join the data from your dictionary into a single file, checkout the merging section of the docs (think of this in terms of pandas DataFrames rather than spreadsheets).

like image 123
Andy Hayden Avatar answered Dec 07 '25 16:12

Andy Hayden



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!