简体   繁体   中英

Pandas Data-Frame : Conditionally select columns

I have a pandas data-frame as shown below...

   C1  C2   C3     C4
0  -1   -3   3   0.75
1  10   20  30  -0.50 

I want to add only the first two columns in each row which have a value less than zero. For example, the series I would get for the above case would be as below...

   CR
0  -4
1  0

I know how to apply other functions like the below...

df.iloc[:, :-2].abs().sum(axis = 1)

Is there a way using lambda functions?

It seems you need select by iloc with where and sum :

df = df.iloc[:,:2].where(df < 0).sum(axis=1)
print (df)
0   -4.0
1    0.0
dtype: float64

If need solution with selection by callable :

df = df.iloc[:, lambda df: [0,1]].where(df < 0).sum(axis=1)
print (df)
0   -4.0
1    0.0
dtype: float64

For lambda function in python is applicable here too.

lambda in pandas :

#sample data
np.random.seed(100)
df = pd.DataFrame(np.random.randint(10, size=(5,5)), columns=list('ABCDE'))
print (df)
   A  B  C  D  E
0  8  8  3  7  7
1  0  4  2  5  2
2  2  2  1  0  8
3  4  0  9  6  2
4  4  1  5  3  4

Get difference by rows .apply(axis=0) what is default so same .apply() :

#instead function f1 is possible use lambda, if function is simple
print (df.apply(lambda x: x.max() - x.min()))
A    8
B    8
C    8
D    7
E    6
dtype: int64

def f1(x):
    #print (x)
    return x.max() - x.min()

print (df.apply(f1))
A    8
B    8
C    8
D    7
E    6
dtype: int64

Get difference by columns .apply(axis=1)

#instead function f2 is possible use lambda, if function is simple
print (df.apply(lambda x: x.max() - x.min(), axis=1))
0    5
1    5
2    8
3    9
4    4
dtype: int64

def f2(x):
    #print (x)
    return x.max() - x.min()

print (df.apply(f2, axis=1))
0    5
1    5
2    8
3    9
4    4
dtype: int64

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