且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Python Pandas:计算每行数据帧中特定值的频率?

更新时间:2022-12-10 09:46:31

你可以为每个条件调用sum1条件很简单,只是一个直接的axis=1 上的 sum,第二次您可以将 df 与 0 值进行比较,然后像以前一样调用 sum:

You can call sum for each condition, the 1 condition is simple just a straight sum on axis=1, for the second you can compare the df against 0 value and then call sum as before:

In [102]:
df['sum_1'] = df[['out1','out2','out3']].sum(axis=1)
df['sum_0'] = (df[['out1','out2','out3']] == 0).sum(axis=1)
df

Out[102]:
                 domain country  out1  out2  out3  sum_0  sum_1
0         oranjeslag.nl      NL     1     0   NaN      1      1
1     pietervaartjes.nl      NL     1     1     0      1      2
2  andreaputting.com.au      AU   NaN     1     0      1      1
3   michaelcardillo.com      US     0     0   NaN      2      0