分组后合并

编程入门 行业动态 更新时间:2024-10-25 06:31:00
本文介绍了分组后合并的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在groupby之后,我在使用pd.merge时遇到了麻烦.这是我的假设:

I'm having trouble using pd.merge after groupby. Here's my hypothetical:

import pandas as pd from pandas import DataFrame import numpy as np df1 = DataFrame({'key': [1,1,2,2,3,3], 'var11': np.random.randn(6), 'var12': np.random.randn(6)}) df2 = DataFrame({'key': [1,2,3], 'var21': np.random.randn(3), 'var22': np.random.randn(3)}) #group var11 in df1 by key grouped = df1['var11'].groupby(df1['key']) # calculate the mean of var11 by key grouped = grouped.mean() print grouped key 1 1.399430 2 0.568216 3 -0.612843 dtype: float64 print grouped.index Int64Index([1, 2, 3], dtype='int64') print df2 key var21 var22 0 1 -0.381078 0.224325 1 2 0.836719 -0.565498 2 3 0.323412 -1.616901 df2 = pd.merge(df2, grouped, left_on = 'key', right_index = True)

这时,我得到IndexError:列表索引超出范围.

At this point, I get IndexError: list index out of range.

使用groupby时,分组变量(在此示例中为键")成为结果序列的索引,这就是为什么我指定"right_index = True"的原因.我尝试了其他语法但没有成功.有什么建议吗?

When using groupby, the grouping variable ('key' in this example) becomes the index for the resultant series, which is why I specify 'right_index = True'. I've tried other syntax without success. Any advice?

推荐答案

我认为您应该这样做:

In [140]: df2 = pd.merge(df2, pd.DataFrame(grouped, columns=['mean']), left_on='key', right_index=True) print df2 key var21 var22 mean 0 1 0.324476 0.701254 0.400313 1 2 -1.270500 0.055383 -0.293691 2 3 0.804864 0.566747 0.628787 [3 rows x 4 columns]

之所以不起作用,是因为grouped是Series而不是DataFrame

The reason it didn't work is that grouped is a Series not a DataFrame

更多推荐

分组后合并

本文发布于:2023-10-17 10:25:31,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1500641.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!