简体   繁体   English

将数据框分组并将数据从几列聚合到一个新列中

[英]Group dataframe and aggregate data from several columns into a new column

I want to group this dataframe by column a , and create a new column ( d ) with all values from both column b and column c .我想按a列对这个数据框进行分组,并创建一个新列 ( d ),其中包含来自b列和c列的所有值。

data_dict = {'a': list('aabbcc'),
             'b': list('123456'),
             'c': list('xxxyyy')}

df = pd.DataFrame(data_dict)

From this...由此...

在此处输入图片说明

to this对此

在此处输入图片说明

I've figured out one way of doing it,我想出了一种方法,

df['d'] = df['b'] + df['c']
df.groupby('a').agg({'d': lambda x: ','.join(x)})

but is there a more pandas way ?但是有更多的熊猫方式吗?

I think "more pandas" is hard to define, but you are able to groupby agg directly on the series if you're trying to avoid the temp column:我认为“更多熊猫”很难定义,但是如果您试图避免使用临时列,则可以直接在系列上对groupby agg进行groupby agg

g = (df['b'] + df['c']).groupby(df['a']).agg(','.join).to_frame('d')

g : g

       d
a       
a  1x,2x
b  3x,4y
c  5y,6y

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将多行中的数据获取到新 dataframe 的不同列中 - How to get data from several rows into different columns of a new dataframe 数据框按几列分组 - Dataframe group by several columns Pandas - 将一个数据框中的列与另一个数据框中的多个列匹配,并从原始数据框创建新列 - Pandas - matching values from a column in one dataframe to several columns in another dataframe and creating new columns from the original dataframe Python group by 数据从一列到一行新建 dataframe - Python group by data from a column to a row in a new dataframe Python:在 Pandas 中,根据条件从数据帧中的几列中提取数据,并添加到列上的不同数据帧匹配中 - Python: In Pandas extract data from several columns in a dataframe based on a condition and add to different dataframe matching on a column Pandas dataframe:根据其他列的数据创建新列 - Pandas dataframe: Creating a new column based on data from other columns 如何使用 DataFrame 内的列中的数据创建新列? - How to create new columns using data from the column inside the DataFrame? 插入几个新列,其值基于 pandas 中 Dataframe 中的另一列 - Insert several new column with the values based on another columns in a Dataframe in pandas 检查熊猫数据框中的几列是否重合并将它们标记在新列中 - Checking if several columns in a panda dataframe coincide and labeling them in a new column 在同一个Pandas DataFrame中的一个新列中合并几个列 - Merging several columns in one new column in the same pandas DataFrame
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM