qed
qed

Reputation: 23104

Use idxmax for indexing in pandas

Here is what I am trying to do:

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"

Intuitively this should work, but it doesn't. Any workarounds?

Upvotes: 1

Views: 1321

Answers (1)

EdChum
EdChum

Reputation: 394003

You should iterate over the series and access the index and col name to set the values:

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

I've printed the items to show what the contents are

Upvotes: 2

Related Questions