【问题标题】:How to get all combinations of three columns in a dataframe in pandas python>如何在pandas python中获取数据框中三列的所有组合>
【发布时间】:2019-08-30 10:51:30
【问题描述】:

三列的所有可能组合

我无法使用 itertools.combinations 或 itertools.permutations 来做到这一点。

输入数据框:

a   b   c
1   101 1001
2   102 1002
3   103 1003

预期的数据框:

a   b   c
1   101 1001
1   101 1002
1   101 1003
1   102 1001
1   102 1002
1   102 1003
1   103 1001
1   103 1002
1   103 1003
2   101 1001
2   101 1002
2   101 1003
2   102 1001
2   102 1002
2   102 1003
2   103 1001
2   103 1002
2   103 1003
3   101 1001
3   101 1002
3   101 1003
3   102 1001
3   102 1002
3   102 1003
3   103 1001
3   103 1002
3   103 1003

【问题讨论】:

    标签: python pandas combinations data-science


    【解决方案1】:

    使用itertools.product:

    from  itertools import product
    #all columns
    df = pd.DataFrame(list(product(*df.values.T)))
    #if you need to specify columns
    #df = pd.DataFrame(list(product(*[df.a, df.b, df.c])))
    print (df)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-09-06
      • 1970-01-01
      • 1970-01-01
      • 2018-12-20
      • 1970-01-01
      • 2013-08-20
      相关资源
      最近更新 更多