简体   繁体   中英

Convert ndarray to dict in python3

I have a ndarray that look like this

LABEL1              99       113           2010-04-26 20:12:23+00:00
LABEL1              29       143           2010-05-06 20:12:23+00:00
LABEL1              99       323           2010-02-12 20:12:23+00:00
LABEL1              23       223           2010-04-25 20:12:23+00:00
LABEL2              23        23           2010-01-21 20:12:23+00:00
LABEL1             234       123           2010-12-26 20:12:23+00:00
LABEL1              93       133           2010-02-23 20:12:23+00:00
LABEL4              19      1223           2010-07-24 20:12:23+00:00

I need to do some aggregation and return as dict..

What I should get at the end is similary to this

[ 
  { 'LABEL1': { 'COLA':577,  'COLB': 1058, 'LAST': '2010-12-26 20:12:23+00:00' } },
  { 'LABEL2': { 'COLA':23,   'COLB': 23,   'LAST': '2010-01-21 20:12:23+00:00' } },
  { 'LABEL4': { 'COLA':19,   'COLB':1223,  'LAST': '2010-07-24 20:12:23+00:00' } }
]

The way I was thinking of doing was to convert to DataFrame, then do a group().agg...

aggr = select_df.groupby('LABELS').agg({'LABELS': [('LABELS', 'max')], 'COLA': [('COLA', 'sum'), ('COLB', 'count')], {'LAST': [('LAST', 'max')]})

I'm kinda new to Python... and having nightmare with all data conversion required to do this...

The original structure is a list

  [
    { 'Label': 'xxxx', 'LABELS': 'xxxx', 'COLA': ##, 'COLB': ##, 'LAST': 'datetime' },...
  ]

If I could simply aggregate directly this list and then concatenate with the next pass (list is read in chunk) to have a final list as mentioned above...

First convert it into dataframe:

df:

    0       1   2   3
0   LABEL1  29  143 2010-05-06  20:12:23+00:00
1   LABEL1  99  323 2010-02-12  20:12:23+00:00
2   LABEL1  23  223 2010-04-25  20:12:23+00:00
3   LABEL2  23  23  2010-01-21  20:12:23+00:00
4   LABEL1  234 123 2010-12-26  20:12:23+00:00
5   LABEL1  93  133 2010-02-23  20:12:23+00:00
6   LABEL4  19  1223    2010-07-24  20:12:23+00:00

df.columns = ['label','x','y','z','w']

df.set_index('label').T.to_dict('dict')

result:

{'LABEL1': {'x': 93, 'y': 133, 'z': '2010-02-23', 'w': '20:12:23+00:00'},
 'LABEL2': {'x': 23, 'y': 23, 'z': '2010-01-21', 'w': '20:12:23+00:00'},
 'LABEL4': {'x': 19, 'y': 1223, 'z': '2010-07-24', 'w': '20:12:23+00:00'}}

Edit: Then groupby label and aggregate by sum, max

df.groupby(["label"])\
    .agg({"x": "sum", "y": "sum", "z": "max", "w": "max"}).T.to_dict('dict')

result:

{'LABEL1': {'x': 478, 'y': 945, 'z': '2010-12-26', 'w': '20:12:23+00:00'},
 'LABEL2': {'x': 23, 'y': 23, 'z': '2010-01-21', 'w': '20:12:23+00:00'},
 'LABEL4': {'x': 19, 'y': 1223, 'z': '2010-07-24', 'w': '20:12:23+00:00'}}

Your attempt was pretty close.

Code:

import pandas as pd

input = [
    {"LABELS": "LABEL1", "COLA": 99, "COLB": 113, "LAST": "2010-04-26 20:12:23+00:00"},
    {"LABELS": "LABEL1", "COLA": 29, "COLB": 143, "LAST": "2010-05-06 20:12:23+00:00"},
    {"LABELS": "LABEL1", "COLA": 99, "COLB": 323, "LAST": "2010-02-12 20:12:23+00:00"},
    {"LABELS": "LABEL1", "COLA": 23, "COLB": 223, "LAST": "2010-04-25 20:12:23+00:00"},
    {"LABELS": "LABEL2", "COLA": 23, "COLB": 23, "LAST": "2010-01-21 20:12:23+00:00"},
    {"LABELS": "LABEL1", "COLA": 234, "COLB": 123, "LAST": "2010-12-26 20:12:23+00:00"},
    {"LABELS": "LABEL1", "COLA": 93, "COLB": 133, "LAST": "2010-02-23 20:12:23+00:00"},
    {"LABELS": "LABEL4", "COLA": 19, "COLB": 1223, "LAST": "2010-07-24 20:12:23+00:00"},
]

df = (
    pd.DataFrame(input)
    .groupby(["LABELS"])
    .agg({"COLA": "sum", "COLB": "sum", "LAST": "max"})
)

print(df.to_dict("index"))

Output:

{'LABEL1': {'COLA': 577, 'COLB': 1058, 'LAST': '2010-12-26 20:12:23+00:00'}, 'LABEL2': {'COLA': 23, 'COLB': 23, 'LAST': '2010-01-21 20:12:23+00:00'}, 'LABEL4': {'COLA': 19, 'COLB': 1223, 'LAST': '2010-07-24 20:12:23+00:00'}}

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