简体   繁体   中英

Python Pandas, how to group list of dict and sort

I have a list of dict like:

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': '-'}
]

How to use Pandas to get data like:

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'}

]

Same ID and same type to one col and sorted by testA and testB values

sorted : both testA and testB have value and lager value of testA+testB at the top.

First convert columns to numeric with replace non numeric to integers and then aggregate 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

If want sorting by sum of both columns use 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     -

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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