I'm trying to create a new column based on other columns existing in my df
.
My new column, col
, should be 1
if there is at least one 1
in columns A ~ E.
If all values in columns A ~ E is 0
, then value of col
should be 0
.
I've attached image for a better understanding.
What is the most efficient way to do this with python, not using loop
? Thanks.
If need test all columns use DataFrame.max
or DataFrame.any
with cast to integers for True/False
to 1/0
mapping:
df['col'] = df.max(axis=1)
df['col'] = df.any(axis=1).astype(int)
Or if need test columns between A:E
add DataFrame.loc
:
df['col'] = df.loc[:, 'A':'E'].max(axis=1)
df['col'] = df.loc[:, 'A':'E'].any(axis=1).astype(int)
If need specify columns by list use subset:
cols = ['A','B','C','D','E']
df['col'] = df[cols].max(axis=1)
df['col'] = df[cols].any(axis=1).astype(int)
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.