簡體   English   中英

Pandas Aggregate groupby

[英]Pandas Aggregate groupby

我有一個概念上看起來如下的數據框:

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'

對於每個組中a我需要統計獨特的(b,c)值高達這里。

所以在這個例子中,ouptut應該是[3,4,4]

(因為在組1中有3個唯一的(b,c)對,並且在組1和組2中共有4個唯一的(b,c)值,並且在組1和2和3中一起也只有4個唯一(b,c)值。

我嘗試使用expandinggroupbynunique但我無法弄清楚語法。

任何幫助將不勝感激!

首先找到唯一行的索引:

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

然后找到每組中剩余行數的累積總和:

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

回國

a
1    3
2    4

我改進了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

這是一個棘手的問題。 這就是你追求的嗎?

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

您可以在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])

如果要在以下列表中轉換結果:

result.tolist()

結果將是[3,2]與你的例子,因為你有3個獨特的情侶,對於組a=1和2個獨特的情侶,對於組a=2

如果你想要colums'b'和'c'的獨特情侶數:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM