python - How can I create a list of records groupedby index in Pandas? -
i have csv of records:
name,credits,email bob,,test1@foo.com bob,6.0,test@foo.com bill,3.0,something_else@a.com bill,4.0,something@a.com tammy,5.0,hello@gmail.org
where name
index. because there multiple records same name, i'd roll entire row (minus name) list create json of form:
{ "bob": [ { "credits": null, "email": "test1@foo.com"}, { "credits": 6.0, "email": "test@foo.com" } ], // ... }
my current solution bit kludgey seems use pandas tool reading csv, nonetheless generates expected jsonish output:
#!/usr/bin/env python3 import io import pandas pd pprint import pprint collections import defaultdict def read_data(): s = """name,credits,email bob,,test1@foo.com bob,6.0,test@foo.com bill,3.0,something_else@a.com bill,4.0,something@a.com tammy,5.0,hello@gmail.org """ data = io.stringio(s) return pd.read_csv(data) if __name__ == "__main__": df = read_data() columns = df.columns index_name = "name" print(df.head()) records = defaultdict(list) name_index = list(columns.values).index(index_name) columns_without_index = [column i, column in enumerate(columns) if != name_index] record in df.values: name = record[name_index] record_without_index = [field i, field in enumerate(record) if != name_index] remaining_record = {k: v k, v in zip(columns_without_index, record_without_index)} records[name].append(remaining_record) pprint(dict(records))
is there way same thing in native pandas (and numpy)?
is want?
cols = df.columns.drop('name').tolist()
or recommended @jezrael:
cols = df.columns.difference(['name'])
and then:
s = df.groupby('name')[cols].apply(lambda x: x.to_dict('r')).to_json()
let's print nicely:
in [45]: print(json.dumps(json.loads(s), indent=2)) { "bill": [ { "credits": 3.0, "email": "something_else@a.com" }, { "credits": 4.0, "email": "something@a.com" } ], "bob": [ { "credits": null, "email": "test1@foo.com" }, { "credits": 6.0, "email": "test@foo.com" } ], "tammy": [ { "credits": 5.0, "email": "hello@gmail.org" } ] }
Comments
Post a Comment