连接 pandas 数据框中的所有列 [英] Concatenating all columns in pandas dataframe

查看:45
本文介绍了连接 pandas 数据框中的所有列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图连接熊猫数据框的所有列,以便最后得到1列,其中包含该数据框的所有值.下面的代码可以做到这一点:

I am trying to concatenate all columns of a pandas dataframe so that I end up with 1 column that contains all the values from the dataframe. The following code does this:

df2 = pd.concat([df[0], df[1], df[2], df[3], df[4], df[5], df[6], df[7]])

但是我希望能够对具有不同列数的数据框执行此操作.当我尝试时:

But I would like to be able to do this with dataframes that have different numbers of columns. When I tried:

dfpr2 = pd.concat([df.columns)

我收到以下错误:无法连接类型为< class'pandas.core.indexes.range.RangeIndex> 的对象;只有Seri​​es和DataFrame objs有效

I get the following error: "cannot concatenate object of type <class 'pandas.core.indexes.range.RangeIndex>; only Series and DataFrame objs are valid

有没有办法解决这个问题?我尝试设置ignore_index = True,但这似乎也无济于事.谢谢!

is there a way to get around this? I tried setting ignore_index = True, but that did not seem to help either. Thanks!!

推荐答案

IIUC df.astype(str).sum(axis = 1)

df = pd.DataFrame({'A' : ['A','B','C'],
             'B' : [0,1,2],
             'C' : ['2019-01-10','2020-01-10','2021-01-10']})

df['hash'] = df.astype(str).sum(axis=1)

print(df)

   A  B           C          hash
0  A  0  2019-01-10  A02019-01-10
1  B  1  2020-01-10  B12020-01-10
2  C  2  2021-01-10  C22021-01-10

如果您需要自定义定界符,请使用 .agg

If you need a custom delimiter then use .agg

df.astype(str).agg('|'.join,axis=1)

0    A|0|2019-01-10
1    B|1|2020-01-10
2    C|2|2021-01-10

这篇关于连接 pandas 数据框中的所有列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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