【问题标题】:All possible combinations of 3 columns in a dataframe数据框中 3 列的所有可能组合
【发布时间】:2018-07-09 13:00:36
【问题描述】:

这是对上一个问题的跟进

combinations of two columns

我正在尝试使用一个数据框并创建另一个数据框,将所有可能的 3 列组合在一起,以及相应值之间的差异,即 11 月 11 日列 ABC 应为 (2B -A - C)= 0,然后 2*BAD = 0 等等。

例如,以

开头
        Dt              A           B           C          D
        11-apr          1           1           1          1
        10-apr          2           3           1          2

如何获得如下所示的新框架:

【问题讨论】:

  • 这里的方法与 jezrael 在您的 previous post 中提供的答案相同 - 您只需将 concat 部分转换为以下内容:df = pd.concat([(2*df[c[1]]).sub(df[c[0]]+df[c[2]]) for c in cc], axis=1, keys=cc)

标签: python pandas itertools


【解决方案1】:

我认为需要:

cc = list(combinations(df.columns,3))

df = pd.concat([df[c[1]].mul(2).sub(df[c[2]]).sub(df[c[0]]) for c in cc], axis=1, keys=cc)
df.columns = df.columns.map(''.join)
print (df)
        ABC  ABD  ACD  BCD
Dt                        
11-apr    0    0    0    0
10-apr    3    2   -2   -3

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-09-06
    • 2019-11-18
    • 2016-08-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多