Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Save pandas pivot_table to include index and columns names

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
like image 346
Cai Avatar asked Mar 26 '19 14:03

Cai


People also ask

How do I give an index name in Pandas?

Use pandas. DataFrame. rename_axis() to set the index name/title, in order to get the index use DataFrame.index.name property and the same could be used to set the index name as well.

Can I store a list in a DataFrame?

You can insert a list of values into a cell in Pandas DataFrame using DataFrame.at() , DataFrame. iat() , and DataFrame. loc() methods.

How will you add an index row or column to a DataFrame in Pandas?

The pandas. concat() method can also be used to add a column to the existing DataFrame by passing axis=1. This method will return the new DataFrame as the output, including the newly added column. Using the index, the above method will concatenate the Series with the original DataFrame.

What is the flatten method in Pandas?

Return a copy of the array collapsed into one dimension. Whether to flatten in C (row-major), Fortran (column-major) order, or preserve the C/Fortran ordering from a . The default is 'C'.


1 Answers

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
like image 165
jezrael Avatar answered Oct 19 '22 10:10

jezrael