簡體   English   中英

Python Pandas,如何對字典列表進行分組和排序

[英]Python Pandas, how to group list of dict and sort

我有一個 dict 列表,如:

data = [
    {'ID': '000681', 'type': 'B:G+',  'testA': '11'},
    {'ID': '000682', 'type': 'B:G+',  'testA': '-'},
    {'ID': '000683', 'type': 'B:G+',  'testA': '13'},
    {'ID': '000684', 'type': 'B:G+',  'testA': '14'},
    {'ID': '000681', 'type': 'B:G+',  'testB': '15'},
    {'ID': '000682', 'type': 'B:G+',  'testB': '16'},
    {'ID': '000683', 'type': 'B:G+',  'testB': '17'},
    {'ID': '000684', 'type': 'B:G+',  'testB': '-'}
]

如何使用 Pandas 獲取如下數據:

data = [
    {'ID': '000683', 'type': 'B:G+',  'testA': '13',  'testB': '17'},
    {'ID': '000681', 'type': 'B:G+',  'testA': '11',  'testB': '15'},
    {'ID': '000684', 'type': 'B:G+',  'testA': '14',  'testB': '-'},
    {'ID': '000682', 'type': 'B:G+',  'testA': '-',  'testB': '16'}

]

與一個 col 相同的ID和相同的type testAtestB值排序

sorted : testAtestB都在頂部有testA+testB值和較大的值。

首先將列轉換為數字,將非數字替換為整數,然后聚合sum

df = pd.DataFrame(data)    
c = ['testA','testB']
df[c] = df[c].apply(lambda x: pd.to_numeric(x, errors='coerce'))

df1 = df.groupby(['ID','type'])[c].sum(min_count=1).sort_values(c).fillna('-').reset_index()
print (df1)
       ID  type testA testB
0  000681  B:G+    11    15
1  000683  B:G+    13    17
2  000684  B:G+    14     -
3  000682  B:G+     -    16

如果Series.argsort兩列的總和排序,請使用Series.argsort

df = pd.DataFrame(data)
c = ['testA','testB']
df[c] = df[c].apply(lambda x: pd.to_numeric(x, errors='coerce'))

df2 = df.groupby(['ID','type'])[c].sum(min_count=1)
df2 = df2.iloc[(-df2).sum(axis=1).argsort()].fillna('-').reset_index()
print (df2)
       ID  type testA testB
0  000683  B:G+    13    17
1  000681  B:G+    11    15
2  000682  B:G+     -    16
3  000684  B:G+    14     -

暫無
暫無

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

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