【问题标题】:idxmax is not working in case of pivot_table - Pandasidxmax 在 pivot_table 的情况下不起作用 - Pandas
【发布时间】:2020-11-21 11:07:02
【问题描述】:

数据集看起来像这样(在 pandas 数据框内)

   Month  Year  Money
0    Jan  2002    615
1    Feb  2002    756
2    Mar  2002    455
3    Apr  2002    645
4    May  2002    669
5    Jun  2002    913
6    Jul  2002    157
7    Aug  2002    217
8    Sep  2002    985
9    Oct  2002    321
10   Nov  2002    847
11   Dec  2002    179
12   Jan  2003    329
13   Feb  2003    717
14   Mar  2003    278
15   Apr  2003    709
16   May  2003    995

所以,我尝试了 pivot

data = df.pivot('Month', 'Year', 'Money')

得到这样的结果:

Year   2002  2003  2004  2005
Month                        
Apr     645   709   178   800
Aug     217   867   515   748
Dec     179   230   121   905
Feb     756   717   879   772
Jan     615   329   896   108
Jul     157   391   429   699
Jun     913   887   422   537
Mar     455   278   934   906
May     669   995   726   324
Nov     847   536   151   195
Oct     321   950   278   173
Sep     985   459   915   437

意图是在单独的 列。

所以,我尝试了这个。

data['Max'] = data[['2002, 2003, 2004, 2005']].idxmax(axis=1)

这以前在简单的数据框上有效。但是在应用 pivot 之后,它向我展示了这个:

KeyError                                  Traceback (most recent call last)
<ipython-input-57-d841277e2032> in <module>()
----> 1 data['Max'] = data[['2002, 2003, 2004, 2005']].idxmax(axis=1)
      2 data.head()

2 frames
/usr/local/lib/python3.6/dist-packages/pandas/core/indexing.py in _validate_read_indexer(self, key, indexer, axis, raise_missing)
   1638             if missing == len(indexer):
   1639                 axis_name = self.obj._get_axis_name(axis)
-> 1640                 raise KeyError(f"None of [{key}] are in the [{axis_name}]")
   1641 
   1642             # We (temporarily) allow for some missing keys with .loc, except in

KeyError: "None of [Index(['2002, 2003, 2004, 2005'], dtype='object', name='Year')] are in the [columns]"

所以,我尝试通过

重置索引
data=pd.pivot_table(df,index=['Month'],columns='Year',values='Money',aggfunc='sum').reset_index()

结果如下所示:

Year Month  2002  2003  2004  2005
0      Apr   645   709   178   800
1      Aug   217   867   515   748
2      Dec   179   230   121   905
3      Feb   756   717   879   772
4      Jan   615   329   896   108
5      Jul   157   391   429   699
6      Jun   913   887   422   537
7      Mar   455   278   934   906
8      May   669   995   726   324
9      Nov   847   536   151   195
10     Oct   321   950   278   173
11     Sep   985   459   915   437

然后我再次应用了相同的代码:

data['Max'] = data[['2002, 2003, 2004, 2005']].idxmax(axis=1)

同样的错误!

KeyError: "None of [Index(['2002, 2003, 2004, 2005'], dtype='object', name='Year')] are in the [columns]"

print(data.columns) 显示 索引(['月', 2002, 2003, 2004, 2005], dtype='object', name='Year')

我错过了什么?

【问题讨论】:

  • 请将您的数据作为文本而不是图像。
  • 好主意。完成@QuangHoang
  • 我认为这与stackoverflow.com/questions/27914360/…中讨论的问题相同
  • @above_c_level 不。相似的。但这个问题是关于找到最高价值的。我必须找到最高值并将列标题分配给特定行。虽然它给了我一些可以尝试的想法。
  • 你是对的。它是相似的,而不是相同的。

标签: python pandas


【解决方案1】:

我想你想要:

data['Max'] = data.idxmax(axis=1)

或者如果你想要特定的年份:

data['Max'] = data[[2002,2003,2004,2005]].idxmax(axis=1)

如果你的Year 是整数,否则:

data['Max'] = data[['2002','2003','2004','2005']].idxmax(axis=1)

而不是用大字符串索引'2002, 2003, 2004, 2005'

输出:

Year   2002  2003  2004  2005   Max
Month                              
Apr     645   709   178   800  2005
Aug     217   867   515   748  2003
Dec     179   230   121   905  2005
Feb     756   717   879   772  2004
Jan     615   329   896   108  2004
Jul     157   391   429   699  2005
Jun     913   887   422   537  2002
Mar     455   278   934   906  2004
May     669   995   726   324  2003
Nov     847   536   151   195  2002
Oct     321   950   278   173  2003
Sep     985   459   915   437  2002

【讨论】:

  • 整数和字符串 :-(
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多