简体   繁体   English

Pandas Aggregate groupby

[英]Pandas Aggregate groupby

I have a dataframe that looks conceptually like the following: 我有一个概念上看起来如下的数据框:

df = pd.DataFrame({
    "a": [1, 1, 1, 2, 2,3],
    "b": ["a", "a", "c", "a", "d","a"],
    "c": ["2", "3", "4", "2", "3","2"]
})

      a    b    c
  0   1   'a'  '2' 
  1   1   'a'  '3'
  2   1   'c'  '4'
  3   2   'a'  '2'
  4   2   'd'  '3'
  5   3   'a'  '2'

For each group in a I need to count the unique (b,c) values up to here. 对于每个组中a我需要统计独特的(b,c)值高达这里。

So in this example the ouptut should be [3,4,4] . 所以在这个例子中,ouptut应该是[3,4,4]

(Because in group 1 there are 3 unique (b,c) pairs, and in group 1 and 2 together there are 4 unique (b,c) values, and in group 1 and 2 and 3 together there are also only 4 unique (b,c) values. (因为在组1中有3个唯一的(b,c)对,并且在组1和组2中共有4个唯一的(b,c)值,并且在组1和2和3中一起也只有4个唯一(b,c)值。

I tried using expanding with groupby and nunique but I couldn't figure out the syntax. 我尝试使用expandinggroupbynunique但我无法弄清楚语法。

Any help will be appreciated! 任何帮助将不胜感激!

First find the indices of the unique rows: 首先找到唯一行的索引:

idx = df[['b','c']].drop_duplicates().index

Then find the cumulative sum of the number of rows left in each group: 然后找到每组中剩余行数的累积总和:

np.cumsum(df.iloc[idx,:].groupby('a').count()['b'])

returning 回国

a
1    3
2    4

I improved Dan's answer. 我改进了Dan的答案。

df['t'] = np.cumsum(~df[['b','c']].duplicated())
df.groupby('a')['t'].last()
Out[44]: 
a
1    3
2    4
3    4
Name: t, dtype: int64

This is a tricky question. 这是一个棘手的问题。 Is this what you are after? 这就是你追求的吗?

result = (
    df.a.drop_duplicates(keep='last')
    .reset_index()['index']
    .apply(lambda x: df.loc[df.index<=x].pipe(lambda x: (x.b+x.c).nunique()))
     )


result
Out[27]: 
0    3
1    4
Name: index, dtype: int64

You can use the drop_duplicates after your groupby and get the shape of the object : 您可以在groupby之后使用drop_duplicates并获取对象的shape

df = pd.DataFrame({
    "a": [1, 1, 1, 2, 2],
    "b": ["a", "a", "c", "a", "d"],
    "c": ["2", "3", "4", "2", "3"]
})
result = df.groupby("a").apply(lambda x: x.drop_duplicates().shape[0])

If you want to convert the result in list after : 如果要在以下列表中转换结果:

result.tolist()

The result will be [3,2] with your example because you have 3 unique couples for group a=1 and 2 unique couples for group a=2 . 结果将是[3,2]与你的例子,因为你有3个独特的情侣,对于组a=1和2个独特的情侣,对于组a=2

If you want the number of unique couple for colums 'b' and 'c' : 如果你想要colums'b'和'c'的独特情侣数:

df[["b", "c"]].drop_duplicates().shape[0]

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM