简体   繁体   中英

How do I convert this Pandas DataFrame into a specific JSON format?

I'm trying to figure out how I can go from this Pandas DataFrame:

熊猫数据框

to this JSON:

JSON

I've tried the dataframe.to_json method (and its various return orientations) but that's not giving me the exact desired output. I've had a good read around online, but everything is very fiddly. Any help would be greatly appreciated :)

The raw dataFrame is here incase:

NETWORK_USERID  TALK_STATUS TALK_STATUS_EXPIRY  CATEGORY    CATEGORY_EXPIRY
0   f40daf16-f069-4c1d-ac2a-d1504f0fc147    Talker  15/12/2020  MN_FFBQ 23/12/2019
1   4d3e9e50-f88b-4c0b-a700-881474f992ab    Lurker  15/12/2020  MN_FFBQ 23/12/2019
2   c2e2fa63-efad-4b7d-b11e-77d9c8692677    Lurker  15/12/2020  MN_FFBQ 23/12/2019
3   c46a2af4-0c20-486e-9ae0-6323269f252d    Lurker  15/12/2020  MN_FFBQ 23/12/2019
4   f6f88be2-dca6-4129-93ed-2b32a633e1ec    Talker  15/12/2020  MN_FFBQ 23/12/2019

There is an apposite function provided by pandas library: to_json function.

Documentation ⬇️

https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.to_json.html

Maybe this can help you :

import pandas as pd
from io import StringIO

data = """
NETWORK_USERID,TALK_STATUS,TALK_STATUS_EXPIRY,CATEGORY,CATEGORY_EXPIRY
f40daf16-f069-4c1d-ac2a-d1504f0fc147,Talker,15/12/2020,MN_FFBQ,23/12/2019
4d3e9e50-f88b-4c0b-a700-881474f992ab,Lurker,15/12/2020,MN_FFBQ,23/12/2019
c2e2fa63-efad-4b7d-b11e-77d9c8692677,Lurker,15/12/2020,MN_FFBQ,23/12/2019
c46a2af4-0c20-486e-9ae0-6323269f252d,Lurker,15/12/2020,MN_FFBQ,23/12/2019
f6f88be2-dca6-4129-93ed-2b32a633e1ec,Talker,15/12/2020,MN_FFBQ,23/12/2019
"""
df = pd.read_csv(StringIO(data),sep=',')

v = df.values.tolist()
lst = []
col1=['value','expiry']

for row in v:
    lst.append([row[0],'['+str(dict(zip(col1, [row[3],row[4]])))+']'])

df1 = pd.DataFrame(lst,columns=['NETWORK_USERID','CATEGORY'])
print(df1)

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