[英]Selecting different rows from different GroupBy groups
As opposed to GroupBy.nth , which selects the same index for each group, I would like to take specific indices from each group. 与GroupBy.nth (为每个组选择相同的索引)相反,我想从每个组中获取特定的索引。 For example, if my GroupBy object consisted of four groups and I would like the 1st, 5th, 10th, and 15th from each respectively, then I would like to be able to pass
x = [0, 4, 9, 14]
and get those rows. 例如,如果我的GroupBy对象由四个组组成,并且我希望分别从第1个,第5个,第10个和第15个组,那么我希望能够传递
x = [0, 4, 9, 14]
并得到这些行。
This is kind of a strange thing to want; 这是一件很奇怪的事情。 is there a reason?
有什么原因吗?
In any case, to do what you want, try this: 无论如何,要执行您想要的操作,请尝试以下操作:
df = pd.DataFrame([['a', 1], ['a', 2],
['b', 3], ['b', 4], ['b', 5],
['c', 6], ['c', 7]],
columns=['group', 'value'])
def index_getter(which):
def get(series):
return series.iloc[which[series.name]]
return get
which = {'a': 0, 'b': 2, 'c': 1}
df.groupby('group')['value'].apply(index_getter(which))
Which results in: 结果是:
group
a 1
b 5
c 7
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.