【问题标题】:Merging more than two columns of the same dataframe in pandas在熊猫中合并同一数据框的两列以上
【发布时间】:2021-06-22 20:39:38
【问题描述】:

尝试重新组织以下数据框,以便 [VAR] 1-3 沿 [GROUP] 列按数字顺序合并

VAR 1   VAR 2   VAR 3   GROUP 
                3   [0-10]
1               3   [0-10]
1               3   [0-10]
1       2           [0-10]
        2           [0-10]
3              3    [10-20]
3       1           [10-20]
        1           [10-20]
        2           [10-20]
               2    [10-20]
               2    [10-20]

试图得到这个作为最终结果:

VAR_MERGED  GROUP 
1           [0-10]
1           [0-10]
1           [0-10]
2           [0-10]
2           [0-10]
3           [0-10]
3           [0-10]
3           [0-10]
1           [10-20]
1           [10-20]
2           [10-20]
2           [10-20]
2           [10-20]
3           [10-20]
3           [10-20]
3           [10-20]

我尝试使用 df['VAR_MERGED'] = df[['VAR 1' 'VAR 2' 'VAR 3']].agg('-'.join, axis=1),但收到有关预期 str 的错误,但 VAR 列中的值都是浮点数,但不确定为什么这需要字符串值?

【问题讨论】:

  • 你能分享一个你的真实数据样本吗? df.head(10) 应该没问题。

标签: python pandas dataframe merge


【解决方案1】:

输入数据:

>>> df
    VAR 1  VAR 2  VAR 3    GROUP  ANOTHER
0     NaN    NaN    3.0   [0-10]  another
1     1.0    NaN    3.0   [0-10]  another
2     1.0    NaN    3.0   [0-10]  another
3     1.0    2.0    NaN   [0-10]  another
4     NaN    2.0    NaN   [0-10]  another
5     3.0    NaN    3.0  [10-20]  another
6     3.0    1.0    NaN  [10-20]  another
7     NaN    1.0    NaN  [10-20]  another
8     NaN    2.0    NaN  [10-20]  another
9     NaN    NaN    2.0  [10-20]  another
10    NaN    NaN    2.0  [10-20]  another

您可以使用melt。要充分理解,可以逐行执行代码(df.melt(...)df.melt(...).dropna()df.melt(...).dropna.sort_values(...)等):

id_vars = df.columns[~df.columns.str.startswith('VAR')]

out = df.melt(id_vars, value_name='VAR_MERGED') \
        .dropna() \
        .sort_values(['GROUP', 'VAR_MERGED']) \
        .reset_index(drop=True) \
        [['VAR_MERGED'] + id_vars]

结果输出:

>>> out
    VAR_MERGED    GROUP  ANOTHER
0          1.0   [0-10]  another
1          1.0   [0-10]  another
2          1.0   [0-10]  another
3          2.0   [0-10]  another
4          2.0   [0-10]  another
5          3.0   [0-10]  another
6          3.0   [0-10]  another
7          3.0   [0-10]  another
8          1.0  [10-20]  another
9          1.0  [10-20]  another
10         2.0  [10-20]  another
11         2.0  [10-20]  another
12         2.0  [10-20]  another
13         3.0  [10-20]  another
14         3.0  [10-20]  another
15         3.0  [10-20]  another

【讨论】:

  • 需要将 int 更改为 float 以供我使用,但这有效,谢谢,但我在 df 中有另一列字符串并得到 ValueError: could not convert string to float: 我该如何处理?如果我排除此列则有效,但不幸的是我需要保留它
  • 谢谢这行得通,但是如果还有另一列字符串怎么办?如何向此添加更多列
  • 我的变量实际上并没有被称为VAR,它们都有差异。名字,所以第一行在这里不起作用,我可以用什么代替?
  • 当我尝试这个时,我得到IndexError: only integers, slices (:), ellipsis (...), numpy.newaxis (None) and integer or boolean arrays are valid indices
【解决方案2】:

一种方式通过set_index/stack

df = (
    df.set_index('GROUP')
    .stack()
    .sort_index(level=[0,1])
    .reset_index(-1,drop=True)
    .reset_index(name = 'Var Merged').iloc[:, ::-1]
)

【讨论】:

  • 交换列 ;-) +1
  • @Corralien 谢谢!!.. 更新了我的答案:)
  • 谢谢这也有效,但有没有办法从 df 添加更多列?
猜你喜欢
  • 2018-12-10
  • 2017-11-26
  • 2019-12-02
  • 2021-04-27
  • 1970-01-01
  • 2013-10-23
相关资源
最近更新 更多