将多列合并为1列[python,pandas] [英] Combine multiple columns into 1 column [python,pandas]

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

问题描述

我有一个2列的pandas数据框: {'A':[1, 2, 3],'B':[4, 5, 6]}

I have a pandas data frame with 2 columns: {'A':[1, 2, 3],'B':[4, 5, 6]}

我想创建一个新列,其中: {'C':[1 4,2 5,3 6]}

I want to create a new column where: {'C':[1 4,2 5,3 6]}

推荐答案

如果您不介意zip对象,则可以使用df['C'] = zip(df.A,df.B). 如果您喜欢元组,则可以使用list()投射zip对象.请参阅此帖子.在这种情况下使用zip非常方便.

If you don't mind zip object, then you can usedf['C'] = zip(df.A,df.B). If you like tuple then you can cast zip object with list(). Please refer to this post. It's pretty handy to use zip in this kind of scenarios.

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

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