从已在 Pandas 中重新索引的数据透视表中选择一列 [英] Selecting a column from a pivot table that has been reindexed in pandas

查看:90
本文介绍了从已在 Pandas 中重新索引的数据透视表中选择一列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个如下所示的数据框:

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()

我正在尝试选择列 df['2020-01'] 但我收到一条错误消息:

I am trying to select the column df['2020-01'] but I am receiving an error message:

密钥错误:'2020-01'

你能帮我理解为什么我不能选择这个列吗?df 似乎不是多索引的.我无法选择任何月份列,但可以选择month_payment"、campaign_name"和month_leadgen".

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.

推荐答案

使用 slice(None) 选择关卡中的所有内容.slice(None) 免除您陈述更深层次的内容.这意味着他们

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

选择

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

这篇关于从已在 Pandas 中重新索引的数据透视表中选择一列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆