Maven Maven - 1 month ago 7
Python Question

Pandas: modify cell values using method chaining

>>> df
a b c ...
0 1 2
1 3 4
. ...


I want the
b
column to be doubled.
I know this method

>>> df['b'] *= 2


Can I do this in a method chaining style ? More pandorable I suppose !
Something like this.

>>> (df.drop('c', 1)
.someMethodToReplaceBColumnValues())


I have lot of methods to clean the data and this is one of the operation and I am stuck at this.
I have come across,
replace
,
set_value
etc. But I am not able to figure out how.

I tried
apply
method also, but it returns only the column for which the operation is applied. So I am getting only the
b
column with apply method. How to approach further ?

Answer

You can try assign:

df = pd.DataFrame({'a':[1,2,3],
                   'b':[4,5,6],
                   'c':[7,8,9]})

print (df)
   a  b  c
0  1  4  7
1  2  5  8
2  3  6  9

print (df.drop('c', 1).assign(b=df.b*2))
   a   b
0  1   8
1  2  10
2  3  12

print (df.drop('c', 1).apply(lambda x: x * 2 if x.name == 'b' else x))
   a   b
0  1   8
1  2  10
2  3  12
Comments