在python pandas中将多个列值合并为一列 [英] Merge multiple column values into one column in python pandas

查看:72
本文介绍了在python pandas中将多个列值合并为一列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个像这样的熊猫数据框:

I have a pandas data frame like this:

   Column1  Column2  Column3  Column4  Column5
 0    a        1        2        3        4
 1    a        3        4        5
 2    b        6        7        8
 3    c        7        7        

我现在想要做的是获取一个包含 Column1 和一个新 columnA 的新数据框.此 columnA 应包含从第 2 列到(到)n(其中 n 是从第 2 列到行尾的列数)的所有值,如下所示:

What I want to do now is getting a new dataframe containing Column1 and a new columnA. This columnA should contain all values from columns 2 -(to) n (where n is the number of columns from Column2 to the end of the row) like this:

  Column1  ColumnA
0   a      1,2,3,4
1   a      3,4,5
2   b      6,7,8
3   c      7,7

我怎样才能最好地解决这个问题?任何意见将是有益的.提前致谢!

How could I best approach this issue? Any advice would be helpful. Thanks in advance!

推荐答案

你可以调用 apply 通过 axis=1apply 行-明智的做法是将 dtype 转换为 strjoin:

You can call apply pass axis=1 to apply row-wise, then convert the dtype to str and join:

In [153]:
df['ColumnA'] = df[df.columns[1:]].apply(
    lambda x: ','.join(x.dropna().astype(str)),
    axis=1
)
df

Out[153]:
  Column1  Column2  Column3  Column4  Column5  ColumnA
0       a        1        2        3        4  1,2,3,4
1       a        3        4        5      NaN    3,4,5
2       b        6        7        8      NaN    6,7,8
3       c        7        7      NaN      NaN      7,7

这里我调用 dropna 来去掉 NaN,但是我们需要再次转换为 int 所以我们不会结束以浮点数作为 str.

Here I call dropna to get rid of the NaN, however we need to cast again to int so we don't end up with floats as str.

这篇关于在python pandas中将多个列值合并为一列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆