Python 3.x 使用idxmax在pandas中建立索引

Python 3.x 使用idxmax在pandas中建立索引,python-3.x,pandas,Python 3.x,Pandas,以下是我试图做的: In [7]: from pandas import DataFrame, Series In [8]: import pandas as pd In [9]: import numpy as np In [10]: df = DataFrame([[1.4, np.nan], [7.1, -4.5], [np.nan, np.nan], [0.75, -1.3]], index=['a', 'b', 'c

以下是我试图做的:

In [7]: from pandas import DataFrame, Series

In [8]: import pandas as pd

In [9]: import numpy as np

In [10]: df = DataFrame([[1.4, np.nan], [7.1, -4.5],
                [np.nan, np.nan], [0.75, -1.3]],
                index=['a', 'b', 'c', 'd'],
                columns=['one', 'two'])
Out[10]:
    one  two
a  1.40  NaN
b  7.10 -4.5
c   NaN  NaN
d  0.75 -1.3

In [11]: df.idxmax()
Out[11]:
one    b
two    d
dtype: object

In [12]: df[df.idxmax()] = -9.99
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-12-018b077daf48> in <module>()
----> 1 df[df.idxmax()] = -9.99

/usr/local/lib/python3.4/site-packages/pandas/core/frame.py in __setitem__(self, key, value)
   2103
   2104         if isinstance(key, (Series, np.ndarray, list, Index)):
-> 2105             self._setitem_array(key, value)
   2106         elif isinstance(key, DataFrame):
   2107             self._setitem_frame(key, value)

/usr/local/lib/python3.4/site-packages/pandas/core/frame.py in _setitem_array(self, key, value)
   2131                     self[k1] = value[k2]
   2132             else:
-> 2133                 indexer = self.ix._convert_to_indexer(key, axis=1)
   2134                 self._check_setitem_copy()
   2135                 self.ix._setitem_with_indexer((slice(None), indexer), value)

/usr/local/lib/python3.4/site-packages/pandas/core/indexing.py in _convert_to_indexer(self, obj, axis, is_setter)
   1141                     if isinstance(obj, tuple) and is_setter:
   1142                         return {'key': obj}
-> 1143                     raise KeyError('%s not in index' % objarr[mask])
   1144
   1145                 return _values_from_object(indexer)

KeyError: "['b' 'd'] not in index"
[7]中的
:从熊猫导入数据帧,系列
在[8]中:导入熊猫作为pd
在[9]中:将numpy作为np导入
在[10]中,df=DataFrame([1.4,np.nan],[7.1,-4.5],
[np.nan,np.nan],[0.75,-1.3],
索引=['a','b','c','d'],
列=['1','2'])
出[10]:
12
a 1.40南
b 7.10-4.5
c楠楠
d 0.75-1.3
在[11]:df.idxmax()中
出[11]:
一个b
两个d
数据类型:对象
在[12]中:df[df.idxmax()]=-9.99
---------------------------------------------------------------------------
KeyError回溯(最近一次呼叫最后一次)
在()
---->1 df[df.idxmax()]=-9.99
/usr/local/lib/python3.4/site-packages/pandas/core/frame.py in_u___设置项__(self、key、value)
2103
2104如果isinstance(键,(系列,np.ndarray,列表,索引)):
->2105 self.\u setitem\u数组(键、值)
2106 elif isinstance(键,数据帧):
2107自置项框(键、值)
/数组中的usr/local/lib/python3.4/site-packages/pandas/core/frame.py(self、key、value)
2131自身[k1]=值[k2]
2132其他:
->2133索引器=self.ix.\u将\u转换为\u索引器(键,轴=1)
2134自我检查设置项目复制()
2135 self.ix._setitem_与_索引器((切片(无),索引器),值)
/usr/local/lib/python3.4/site-packages/pandas/core/index.py in_convert_to_indexer(self、obj、axis、is_setter)
1141如果isinstance(对象,元组)和is_setter:
1142返回{'key':obj}
->1143 raise KeyError(“%s”不在索引“%objarr[mask]”中)
1144
1145从对象(索引器)返回值
KeyError:“['b'd']不在索引中”

直觉上这应该是可行的,但事实并非如此。任何解决方法?

您应该迭代序列并访问索引和列名称以设置值:

In [30]:

for items in df.idxmax().iteritems():
    print(items)
    df.loc[items[1], items[0]] = -9.9
df
('one', 'b')
('two', 'd')
Out[30]:
    one  two
a  1.40  NaN
b -9.90 -4.5
c   NaN  NaN
d  0.75 -9.9

我已经打印了这些项目,以显示内容是什么

您是否试图找到等于-9.99的行?我正在尝试修改这些spotsOk,因此7.10和-1.3都将变为-9.99?是的,没错。但在一个完美的世界里,检查平等也应该起作用
df[index]
选择列,而不是行
df.idxmax()
返回值为行标签的序列