Save pandas pivot_table to include index and columns names
Asked Answered
C

1

6

I want to save a pandas pivot table for human reading, but DataFrame.to_csv doesn't include the DataFrame.columns.name. How can I do that?

Example:

For the following pivot table:

>>> import pandas as pd
>>> df = pd.DataFrame([[1, 2, 3], [6, 7, 8]])
>>> df.columns = list("ABC")
>>> df.index = list("XY")
>>> df
   A  B  C
X  1  2  3
Y  6  7  8
>>> p = pd.pivot_table(data=df, index="A", columns="B", values="C")

When viewing the pivot table, we have both the index name ("A"), and the columns name ("B").

>>> p
B    2    7
A
1  3.0  NaN
6  NaN  8.0

But when exporting as a csv we lose the columns name:

>>> p.to_csv("temp.csv")

===temp.csv===
A,2,7
1,3.0,
6,,8.0

How can I get some kind of human-readable output format which contains the whole of the pivot table, including the .columns.name ("B")?

Something like this would be fine:

B,2,7
A,,
1,3.0,
6,,8.0
Carminacarminative answered 26/3, 2019 at 14:37 Comment(5)
save then into excelBluet
@Wen-Ben Could you be more specific?Carminacarminative
pandas.pydata.org/pandas-docs/stable/reference/api/…Bluet
@Wen-Ben That gets me the exact same thing as an .xls, not including the "B".Carminacarminative
How working p.to_csv('temp.csv', index_label=[p.index.name, p.columns.name]) ?Outpouring
O
7

Yes, it is possible by append helper DataFrame, but reading file is a bit complicated:

p1 = pd.DataFrame(columns=p.columns, index=[p.index.name]).append(p)
p1.to_csv('temp.csv',index_label=p.columns.name)
B,2,7
A,,
1,3.0,
6,,8.0

#set first column to index
df = pd.read_csv('temp.csv', index_col=0)
#set columns and index names
df.columns.name = df.index.name
df.index.name = df.index[0]
#remove first row of data
df = df.iloc[1:]
print (df)
B    2    7
A          
1  3.0  NaN
6  NaN  8.0
Outpouring answered 26/3, 2019 at 14:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.