查找数据框的所有可能组合

时间:2018-11-02 17:08:46

标签: python pandas counter pandas-groupby

我有这样的数据:

     Price  Web  Destinations  Airport  Flight  Afterflight  Global
0      1    1             0        0       0            0       0
1      1    1             1        1       1            1       1
2      1    1             1        1       0            1       1
3      0    1             0        0       0            0       0
4      0    0             0        0       0            0       0 

,我想找到除Global以外的所有变量组合,并计算每个组合的实例数。有人可以帮我吗?

1 个答案:

答案 0 :(得分:3)

您可以使用GroupBy + size

res = df.groupby(df.columns[:-1].tolist()).size().rename('Count').reset_index()

print(res)

   Price  Web  Destinations  Airport  Flight  Afterflight  Count
0      0    0             0        0       0            0      1
1      0    1             0        0       0            0      1
2      1    1             0        0       0            0      1
3      1    1             1        1       0            1      1
4      1    1             1        1       1            1      1

您的示例并不有趣,因为所有组合都是唯一的。

相关问题