Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pandas: set values with (row, col) indices

Tags:

python

pandas

pandas offers the ability to look up by lists of row and column indices,

In [49]: index = ['a', 'b', 'c', 'd']

In [50]: columns = ['one', 'two', 'three', 'four']

In [51]: M = pandas.DataFrame(np.random.randn(4,4), index=index, columns=columns)

In [52]: M
Out[52]: 
        one       two     three      four
a -0.785841 -0.538572  0.376594  1.316647
b  0.530288 -0.975547  1.063946 -1.049940
c -0.794447 -0.886721  1.794326 -0.714834
d -0.158371  0.069357 -1.003039 -0.807431

In [53]: M.lookup(index, columns) # diagonal entries
Out[53]: array([-0.78584142, -0.97554698,  1.79432641, -0.8074308 ])

I would like to use this same method of indexing to set M's elements. How can I do this?

like image 642
duckworthd Avatar asked Aug 23 '12 21:08

duckworthd


2 Answers

Multiple years have passed since this answer was written so I though I might contribute a little bit. With the refactoring of pandas, attempting to set a value at a location with

M.iloc[index][col]

May give you a warning about trying to set a value in a slice.

SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame

See the the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy

In pandas versions after 0.21 the correct "pythonic" way is now the pandas.DataFrame.at operator

which looks like this:

M.at[index,col] = new_value

Answer for older versions: the more "pythonic" way to do this in older versions is with the pandas.DataFrame.set_value instruction. Note that this instruction returns the resulting DataFrame.

M.set_value(index,column,new_value)

I just thought I'd post this here after figuring out the source of the warnings that can be generated by the .iloc or .ix approaches.

The set_value approach also works for multiindex DataFrames by putting the multiple levels of the index in as a tuple (e.g. replacing column with (col,subcol) )

like image 175
Ezekiel Kruglick Avatar answered Nov 14 '22 15:11

Ezekiel Kruglick


I'm not sure I follow you, but do you use DataFrame.ix to select/set individual elements:

In [79]: M
Out[79]: 
        one       two     three      four
a -0.277981  1.500188 -0.876751 -0.389292
b -0.705835  0.108890 -1.502786 -0.302773
c  0.880042 -0.056620 -0.550164 -0.409458
d  0.704202  0.619031  0.274018 -1.755726

In [75]: M.ix[0]
Out[75]: 
one     -0.277981
two      1.500188
three   -0.876751
four    -0.389292
Name: a

In [78]: M.ix[0,0]
Out[78]: -0.27798082190723405

In [81]: M.ix[0,0] = 1.0

In [82]: M
Out[82]: 
        one       two     three      four
a  1.000000  1.500188 -0.876751 -0.389292
b -0.705835  0.108890 -1.502786 -0.302773
c  0.880042 -0.056620 -0.550164 -0.409458
d  0.704202  0.619031  0.274018 -1.755726

In [84]: M.ix[(0,1),(0,1)] = 1

In [85]: M
Out[85]: 
        one       two     three      four
a  1.000000  1.000000 -0.876751 -0.389292
b  1.000000  1.000000 -1.502786 -0.302773
c  0.880042 -0.056620 -0.550164 -0.409458
d  0.704202  0.619031  0.274018 -1.755726

You can also slice by indices:

In [98]: M.ix["a":"c","one"] = 2.0

In [99]: M
Out[99]: 
        one       two     three      four
a  2.000000  1.000000 -0.876751 -0.389292
b  2.000000  1.000000 -1.502786 -0.302773
c  2.000000 -0.056620 -0.550164 -0.409458
d  0.704202  0.619031  0.274018 -1.755726
like image 32
reptilicus Avatar answered Nov 14 '22 15:11

reptilicus