[英]Create a column using Pandas to check if sum of a column for one category is greater than the other category
[英]How can I sum up 3 columns specifically using pandas for below data & create new column to flag if it's greater than 0 or equal to 0
對於以下數據,我想創建列,如果 A+B+C 的總和 = 0,那么我想標記“NoCommit”,如果它大於 0,我想標記“Commit”
revno author msg A B C
3030 rohit modified 0 1 0
3031 rohit Statistical Report changes Done 0 2 0
3032 sandeep OTPIntegration flow changed 0 0 0
3033 sandeep Captcha code Integration Done. 0 0 0
使用numpy.where
和sum
:
df['new'] = np.where(df[['A','B','C']].sum(axis=1).eq(0),'NoCommit','Commit')
或者:
df['new'] = np.where((df.A + df.B + df.C).eq(0),'NoCommit','Commit')
編輯:
如果列不是數字,請使用:
df['new'] = np.where(df[['A','B','C']].astype(float).sum(axis=1).eq(0),'NoCommit','Commit')
天真的方法:
df = pd.DataFrame(data, columns=['A', 'B', 'C']
df['Commit'] = df['NoCommit'] = None
df.loc[:,['Commit', 'NoCommit']] = \
[ (1,0) if i > 0 else (0,1) for i in df[['A','B','C']].sum(axis=1)]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.