简体   繁体   中英

Pandas Dataframe: assign to different columns based on positive/negative values

What I want to achieve is as follows:

   a  b  c
0  1  0  0
1 -1  0  0

with the above dataframe, regarding column a , for positive rows, assign to corresponding rows in column b , for negative values, assign to column c :

   a  b  c
0  1  1  0
1 -1  0 -1

I am now using following code, but is there any way I can write it in one single line instead of two?

import pandas as pd
import numpy as np
df = pd.DataFrame({'a': [1, -1], 'b':[0, 0], 'c':[0,0]})
df.b = np.where(df.a > 0, df.a, df.b)
df.c = np.where(df.a < 0, df.a, df.c)

I think using np.where just fine , if you want them in one signle line

s=df.assign(key=['b','c']).set_index('key',append=True).unstack().sum(level=1,axis=1)
s
key    b    c
0    1.0  0.0
1    0.0 -1.0
df.update(s)
df
   a    b    c
0  1  1.0  0.0
1 -1  0.0 -1.0

I started by writing out

df.b, df.c = (df.a > 0)*df.a + (df.a < 0)*df.b, (df.a < 0)*df.a +  (df.a > 0)*df.c

then realized you could do the same unpacking with exactly what you had. There are any number of ways you can do this in more obscure ways, I guess

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM