【问题标题】:Finding all the possible combinations of a Dataframe查找数据框的所有可能组合
【发布时间】:2018-11-02 17:08:46
【问题描述】:

我有这样的数据:

     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 

我想找到除全局变量之外的所有可能的变量组合,并计算每个组合的实例数。谁能帮帮我?

【问题讨论】:

  • 到目前为止你有没有尝试过,你能发布想要的输出吗?
  • 我想成为这样的人:1 1 1 1 1 1 number:43 1 1 1 1 0 0 number:21,就像一个真值表

标签: python pandas counter pandas-groupby


【解决方案1】:

你可以使用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

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

【讨论】:

  • list(itertools.combinations(df['colname'], 2))怎么样
  • @pygo,可能,但我喜欢(如果我知道如何)坚持使用 Pandas 方式,因为它更适合使用 NumPy。
  • @jpp 它实际上工作得很好,并且完全符合我的要求。谢谢大家
  • @TKir,如果这有效,请accept the answer(左边的绿色勾号)让其他用户知道。
猜你喜欢
  • 1970-01-01
  • 2011-05-20
  • 1970-01-01
  • 1970-01-01
  • 2011-08-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多