问题
I have a dataframe that looks like this:
df = pd.DataFrame({'publisher': ['facebook', 'facebook', 'facebook', 'google', 'google', 'google'],
'month_leadgen': ['2019-01', '2019-02', '2019-03', '2019-01', '2019-02', '2019-03'],
'month_payment': ['2019-01', '2019-02', '2019-03', '2019-01', '2019-02', '2019-03'],
'revenue': [60, 150, 450, 85, 250, 150]})
I then created a pivot table:
df = df.pivot_table(index=['publisher', 'month_leadgen'], columns='month_payment', values='revenue').reset_index()
I am trying to select the column df['2020-01'] but I am receiving an error message:
KeyError: '2020-01'
Can you help me understand why I cannot select this column? The df doesn't seem to be multi indexed. I cannot select any of the month columns but 'month_payment', 'campaign_name', and 'month_leadgen' can be selected no problem.
回答1:
Use slice(None) to select all the contents the level. slice(None) exempts you from stating contents of the deeper level. it implies them
df=df.pivot_table(index=['publisher', 'month_leadgen'], columns=['month_payment'], values=['revenue']).reset_index()
print(df)
publisher month_leadgen revenue
month_payment 2019-01 2019-02 2019-03
0 facebook 2019-01 60.0 NaN NaN
1 facebook 2019-02 NaN 150.0 NaN
2 facebook 2019-03 NaN NaN 450.0
3 google 2019-01 85.0 NaN NaN
4 google 2019-02 NaN 250.0 NaN
5 google 2019-03 NaN NaN 150.0
Selection
df.loc[:, (slice(None), '2019-01')]
revenue
month_payment 2019-01
0 60.0
1 NaN
2 NaN
3 85.0
4 NaN
5 NaN
来源:https://stackoverflow.com/questions/63819059/selecting-a-column-from-a-pivot-table-that-has-been-reindexed-in-pandas