Home > Blockchain >  Dataframe not properly writing to excel file
Dataframe not properly writing to excel file

Time:10-19

I have the below dataframe:

  Customer Code 05-01-2021 06-01-2021 07-01-2021 08-01-2021 09-01-2021 10-01-2021 11-01-2021  ... 02-01-2022 03-01-2022 04-01-2022 05-01-2022 06-01-2022 07-01-2022 08-01-2022 09-01-2022
0        C04209        NaN        NaN     132.25      579.0    1228.49        NaN    1978.08  ...    2060.65    1178.16    1563.33    2047.14    1053.51    4111.52     486.42    2337.64
1        C04210        NaN        NaN      430.0        NaN        NaN        NaN        NaN  ...    4679.19     8637.2      591.1     5161.1      720.7    9461.29     6498.0     7595.0 

which is correct but when I try to write to excel with to_excel command this is what i get:

df.to_excel(writer, sheet_name=e, index=False)
writer.save()

the Excel file:

enter image description here

As you can see everything gets shifted by 1 column to the left, does anyone know why and how to fix. Thanks in advance.

CodePudding user response:

Maybe try to identify the index as that first column?

df.to_excel(writer, sheet_name=e, index=0) writer.save()

CodePudding user response:

You could also try using the first column as an index

df2 = df.copy()
df2 = df2.drop("Customer Code", axis=1)
df2.index = df['Customer Code']
df2.to_excel(writer, sheet_name=3, index=False)

  • Related