Home > Mobile >  when I export dataframe to excel I got just part of columns
when I export dataframe to excel I got just part of columns

Time:10-05

I have nested dictionary which I try to export to excel I have code which generate correct dataframe, the idea is to generate it in excel, but I get in excel only part of the columns, what I am doing wrong

user_ids = []
frames = []

for user_id, d in res_duplicate.items():
    for item in d:
        user_ids.append(user_id)
        frames.append(pd.DataFrame.from_dict(item, orient='index'))

df = pd.concat(frames, keys=user_ids)
print (df)
df.to_excel('C:/Users/user/Desktop/dict1.xlsx', index = False, header=False)

when I print dataframe I got

enter image description here

but in excel I have just columns time and docs

CodePudding user response:

First 2 columns are MultiIndex, so if use index=False is not written to excel. Possible solution is remove it:

df = pd.concat(frames, keys=user_ids)
df.to_excel('C:/Users/user/Desktop/dict1.xlsx', header=False)

Or convert MultiIndex to columns, then default RangeIndex is by index=False omitted:

df = pd.concat(frames, keys=user_ids).reset_index()
df.to_excel('C:/Users/user/Desktop/dict1.xlsx',index = False,  header=False)
  • Related