Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import json
- import pandas as pd
- df = pd.read_csv('txn_data.csv')
- def get_nested_rec(key, grp):
- rec = {}
- rec['date'] = key[0]
- rec['name'] = key[1]
- rec['value'] = key[2]
- for field in ['name','value']:
- rec[field] = list(grp[field].unique())
- return rec
- records = []
- for key, grp in df.groupby(['date']):
- rec = get_nested_rec(key, grp)
- records.append(rec)
- records = dict(data = records)
- print(json.dumps(records, indent=4))
- date,name,value
- 1/1/13,Quick Serve,304127
- 1/1/13,Restaurant,1843286
- 1/1/13,Retail,239675
- 1/2/13,Quick Serve,422847
- 1/2/13,Restaurant,1582848
- 1/2/13,Retail,394358
- desired_output = [
- {
- "date":"2017-01-01",
- "details":[
- {
- "name":"Retail",
- "value":9192
- },
- {
- "name":"Restaurant",
- "value":6753
- },
- {
- "name":"Quickserve",
- "value":1219
- }
- ]
- },
- {
- "date":"2017-02-01",
- "details":[
- {
- "name":"Retail",
- "value":9192
- },
- {
- "name":"Restaurant",
- "value":6753
- },
- {
- "name":"Quickserve",
- "value":1219
- }
- ]
- }
- ]
- {
- "data": [
- {
- "date": "1",
- "name": [
- "Automotive",
- "Durable Goods",
- "Entertainment",
- "Food",
- "Lodging",
- "Petroleum",
- "Quick Serve",
- "Restaurant",
- "Retail",
- "Service",
- "Transportation & Utilities",
- "Unknown"
- ],
- "value": [
- 91406,
- 9889,
- 172676,
- 358922,
- 63502,
- 1982048,
- 304127,
- 1843286,
- 239675,
- 106462,
- 25924,
- 909
- ]
- },
- {
- "date": "1",
- "name": [
- "Automotive",
- "Durable Goods",
- "Entertainment",
- "Food",
- "Lodging",
- "Petroleum",
- "Quick Serve",
- "Restaurant",
- "Retail",
- "Service",
- "Transportation & Utilities",
- "Unknown"
- ],
- "value": [
- 146041,
- 33090,
- 103159,
- 336956,
- 66726,
- 2191346,
- 422847,
- 1582848,
- 394358,
- 339989,
- 49477,
- 494
- ]
- }
- ]
- }
Add Comment
Please, Sign In to add comment