Skip to content Skip to sidebar Skip to footer

Pandas Pivot_table Column Names

For a dataframe like this: d = {'id': [1,1,1,2,2], 'Month':[1,2,3,1,3],'Value':[12,23,15,45,34], 'Cost':[124,214,1234,1324,234]} df = pd.DataFrame(d) Cost Month Value id

Solution 1:

Using clues from @chrisb's answer, this gave me exactly what I was after:

df2.reset_index(inplace=True)

which gives:

idCost1Cost2Cost3Value1Value2Value311242141234    12231521324       023445034

and in case of multiple index columns, this post explains it well. just to be complete, here is how:

df2.columns = [' '.join(col).strip() for col in df2.columns.values]

Solution 2:

'id' is the index name, which you can set to None to remove.

In [35]: df2.index.name =NoneIn [36]: df2
Out[36]: 
   Cost1  Cost2  Cost3  Value1  Value2  Value3
1124214123412231521324023445034

Post a Comment for "Pandas Pivot_table Column Names"