Issue
I am struggling with figuring out how to return a conditional value to a column based on values in selected columns on same index row. see the attached picture.
I have a df where "<0" column is supposed to count the number of instances in previous 18 columns where valueas are less than 0.
I also need to count the total number of columns excluding NaN for each row.
any suggestions?
Solution
You can use:
s1 = df.lt(0).sum(axis=1)
s2 = df.notna().sum(axis=1)
df['<0'] = s1
df['TotCount (ex Nan)'] = s2
Or:
cols = df.columns
df['<0'] = df[cols].lt(0).sum(axis=1)
df['TotCount (ex Nan)'] = df[cols].notna().sum(axis=1)
Answered By - mozway
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.