I have a dataframe df:
domain country out1 out2 out3
oranjeslag.nl NL 1 0 NaN
pietervaartjes.nl NL 1 1 0
andreaputting.com.au AU NaN 1 0
michaelcardillo.com US 0 0 NaN
我希望定义两列 sum_0 和 sum_1,并计算每行列(out1,out2,out3)中 0 和 1 的数量。因此,预期结果如下:
domain country out1 out2 out3 sum_0 sum_1
oranjeslag.nl NL 1 0 NaN 1 1
pietervaartjes.nl NL 1 1 0 1 2
andreaputting.com.au AU NaN 1 0 1 1
michaelcardillo.com US 0 0 NaN 2 0
我有一个计算1的数量的代码,但是我不知道如何计算0的数量。
df['sum_1'] = df[['out_1','out_2','out_3']].sum(axis=1)
有人可以帮忙吗?
df[['out1','out2','out3']].isin(df['out1']).sum(axis=1)
- Charles Crous