2014-12-29 77 views
1

這裏idxmax索引是什麼,我試圖做的:使用在熊貓

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" 

直覺上這應該工作,但事實並非如此。任何解決方法?

+0

你試圖找到行那等於-9.99? –

+0

我想修改這些點 – qed

+0

好吧,那麼7.10和-1.3都會變成-9.99? –

回答

2

你應該遍歷系列和訪問索引和col名稱設定值:

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 

我打印的項目顯示什麼內容是