我如何使用pandas为以下数据总结3列,并创建新列以标记其是否大于0或等于0

2024-05-16 20:18:41 发布

您现在位置:Python中文网/ 问答频道 /正文

对于下面的数据,我想创建列,如果A+B+C之和=0,那么我想标记为“NoCommit” 如果大于0,我想标记“提交”

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

Tags: 数据标记reportmsgflowauthormodifiedrohit
2条回答

^{}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)]

相关问题 更多 >