使用idxmax在pandas中建立索引

时间:2014-12-29 22:09:30

标签: 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', '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"

直观地说这应该有效,但事实并非如此。任何解决方法?

1 个答案:

答案 0 :(得分:2)

您应该遍历系列并访问索引和列名以设置值:

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

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