![](/img/trans.png)
[英]Python Pandas: Efficiently compute count of rows with value greater than or equal to a set of values, grouped by key columns
[英]Filter rows with more than 1 value in a set and count their occurrence pandas python
假設,我有以下數據框。
Id Combinations
1 (A,B)
2 (C,)
3 (A,D)
4 (D,E,F)
5 (F)
我想過濾掉集合中超過值的Combination
列值。 像下面的東西。 而且我想在Combination
列中計算整體出現的次數。 例如,應刪除ID
號2
和5
,因為它們在集合中的值僅為 1。
我正在尋找的結果是:
ID Combination Frequency
1 A 2
1 B 1
3 A 2
3 D 2
4 D 2
4 E 1
4 F 2
任何人都可以幫助在 Python pandas 中獲得上述結果嗎?
如有必要,首先將值轉換為列表:
df['Combinations'] = df['Combinations'].str.strip('(,)').str.split(',')
如果在boolean indexing
中僅通過Series.str.len
過濾一個值后需要計數,則使用DataFrame.explode
並通過Series.map
和Series.value_counts
:valuescounts 計數值。
df1 = df[df['Combinations'].str.len().gt(1)].explode('Combinations')
df1['Frequency'] = df1['Combinations'].map(df1['Combinations'].value_counts())
print (df1)
Id Combinations Frequency
0 1 A 2
0 1 B 1
2 3 A 2
2 3 D 2
3 4 D 2
3 4 E 1
3 4 F 1
或者,如果在刪除它們之前需要計數,則在最后一步中通過Series.duplicated
過濾它們:
df2 = df.explode('Combinations')
df2['Frequency'] = df2['Combinations'].map(df2['Combinations'].value_counts())
df2 = df2[df2['Id'].duplicated(keep=False)]
選擇:
df2 = df2[df2.groupby('Id').Id.transform('size') > 1]
或者:
df2 = df2[df2['Id'].map(df2['Id'].value_counts() > 1]
print (df2)
Id Combinations Frequency
0 1 A 2
0 1 B 1
2 3 A 2
2 3 D 2
3 4 D 2
3 4 E 1
3 4 F 2
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.