mythicalcoder
mythicalcoder

Reputation: 3291

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 ?

Upvotes: 3

Views: 2142

Answers (2)

jezrael
jezrael

Reputation: 862511

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

Upvotes: 5

Niels
Niels

Reputation: 192

You can use apply, with a custom function:

def mult(x, columns):
    if x.name in columns:
        return x*2
    else:
        return x

print df.apply(mult, args=(['b', 'c'],))

   a   b  c
0  0   0  0
1  1   2  2
2  2   4  4

Do take into account that you pass a tuple as extra argument to the function in apply!

Upvotes: 1

Related Questions