Reputation: 301
pivot a dataframe using this line:
month_df = pd.pivot_table(loadexpense_df,index="Category",columns="Month",aggfunc={"Month":len}, fill_value=0)
The end result as display below:
Q1: How do I write a logic to change the column name ('Month,'Apr) to Apr only and to the rest of column headers?
Q2: Also can pivot table return only the month as header (eg: Apr, May, etc) instead of the tuple (eg: ('Month,'Apr), ('Month,'Aug), etc?
Thanks
Upvotes: 0
Views: 770
Reputation: 3308
After getting result of pd.pivot_table()
function you get a Dataframe with a columns which represents MultiIndex
with two levels. You can solve both of your problems by setting second level of your MultiIndex
to Dataframe column labels:
month_df = pd.pivot_table(loadexpense_df,
index="Category",
columns="Month",
aggfunc={"Month":len},
fill_value=0)
month_df.columns = month_df.columns.get_level_values(level=1)
Upvotes: 2
Reputation: 747
Q1: Table column-names is contained in month_df.columns
, and they are iterable. So:
columns = month_df.columns
tmp = []
for col in columns:
if len(col) == 2:
tmp.append(col[1]) #Taking second tuple-value
else:
tmp.append(col)
month_df.columns = tmp #Reassigning columns
Q2: I was unable to reproduce your issue with the code given. Mine printed out just fine from the beginning, but I still answered Q1 as I could generate it by hand. But the code
month_df = pd.pivot_table(
loadexpense_df,index="Category",
columns="Month",aggfunc={"Month":len},
fill_value=0)
Gives me only the name of the months as column-names, and not a tuple.
Upvotes: 1