2024-03-28 11:50:44 发布
网友
如果我有一个数据帧
A B C D 1 1 2 2 1 2 1 1 2 1 3 3 1 0 1 4 2 4 4 4
我想把B列和C列相加,计算D列的值是否相同。期望输出为
A B C B+C D 1 1 2 2 4 1 2 1 1 2 3 1 3 3 1 0 1 1 4 2 4 4 8 4 There are 3 different values compare the "B+C" and "D".
你能帮我一下吗?你知道吗
df.insert(3,'B+C', df['B']+df['C']) 3 is the index df.head() A B C B+C D 0 1 2 2 4 1 1 1 1 2 3 1 2 3 1 0 1 1 3 2 4 4 8 4
之后你可以按照@yatu的步骤
df['B+C'].ne(df['D']) 0 True 1 True 2 False 3 True dtype: bool df['B+C'].ne(df['D']).sum() 3
你可以这样做:
df.B.add(df.C).ne(df.D).sum() # 3
如果需要添加列:
df['B+C'] = df.B.add(df.C) diff = df['B+C'].ne(df.D).sum() print(f'There are {diff} different values compare the "B+C" and "D"') #There are 3 different values compare the "B+C" and "D"
之后你可以按照@yatu的步骤
你可以这样做:
如果需要添加列:
相关问题 更多 >
编程相关推荐