【问题标题】:Pandas combinations with format带有格式的熊猫组合
【发布时间】:2018-08-06 04:10:58
【问题描述】:

我的数据集是这样的,

Col1    Col2    Col3
A       10      x1
B       100     x2
C       1000    x3

这就是我希望输出的样子,

Col1    Col2    Col3    Col4    Col5    Col6    Col7    Col8    Col9
A       10      x1      Empty   Empty   Empty   Empty   Empty   Empty
B       100     x2      Empty   Empty   Empty   Empty   Empty   Empty
C       1000    x3      Empty   Empty   Empty   Empty   Empty   Empty
A       10      x1      B       100     x2      Empty   Empty   Empty
B       100     x2      C       1000    x3      Empty   Empty   Empty
A       10      x1      B       100     x2      C       1000    x3

我可以通过 itertools.combinations 获得 A、B、C 的各种组合,但我如何获得这张表?

【问题讨论】:

    标签: python python-3.x pandas csv itertools


    【解决方案1】:

    使用 itertools.combinationsitertools.chain.from_iterable

    arr = list(itertools.chain.from_iterable(
        [[j for i in el for j in i] for el in itertools.combinations(df.values.tolist(), i)]
        for i in range(1, len(df)+1)
        )
    )
    
    pd.DataFrame(arr)
    
       0     1   2     3       4     5     6       7     8
    0  A    10  x1  None     NaN  None  None     NaN  None
    1  B   100  x2  None     NaN  None  None     NaN  None
    2  C  1000  x3  None     NaN  None  None     NaN  None
    3  A    10  x1     B   100.0    x2  None     NaN  None
    4  A    10  x1     C  1000.0    x3  None     NaN  None
    5  B   100  x2     C  1000.0    x3  None     NaN  None
    6  A    10  x1     B   100.0    x2     C  1000.0    x3
    

    另一个使用concat的选项:

    out = pd.concat(
              [pd.DataFrame(list(itertools.combinations(df.values.tolist(), i)))
              for i in range(1, len(df)+1)]
    )
    
    out.applymap(lambda x: [] if type(x) == float else x).sum(1).apply(pd.Series)
    
       0     1   2    3       4    5    6       7    8
    0  A    10  x1  NaN     NaN  NaN  NaN     NaN  NaN
    1  B   100  x2  NaN     NaN  NaN  NaN     NaN  NaN
    2  C  1000  x3  NaN     NaN  NaN  NaN     NaN  NaN
    0  A    10  x1    B   100.0   x2  NaN     NaN  NaN
    1  A    10  x1    C  1000.0   x3  NaN     NaN  NaN
    2  B   100  x2    C  1000.0   x3  NaN     NaN  NaN
    0  A    10  x1    B   100.0   x2    C  1000.0   x3
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-06-05
      • 2018-11-23
      • 2019-11-06
      • 1970-01-01
      • 2021-11-12
      • 2019-03-10
      • 2019-08-08
      • 2020-06-17
      相关资源
      最近更新 更多