简体   繁体   中英

How to extract data from a JSON column from database directly to a pandas dataframe

Hi I am new to Pandas in Python and I am trying to extract a JSON data from a particular table in a database. Could someone help me build the dataframe to extract the required data.

Given below are the details. Table name : Employees Column name : Profile

Sample column:

{"EmpID":"1","Department":"Support","Role":{"Support":1,"Troubleshoot":2,"Update KBarticles":3},"Team_name":Royals,"Date_Joined":" "}

I need to have the data from the above in separate columns. EmpID, Role(Support), Role(Troubleshoot), Role(Update KB),Team_Name, Date_Joined

Could someone help me build the dataframe for the above, thanks.

You could use pandas json normalizer:

import pandas as pd
import json

example = '{"EmpID":"1","Department":"Support","Role":{"Support":1,"Troubleshoot":2,"Update KBarticles":3},"Team_name":"Royals","Date_Joined":" "}'

pd.io.json.json_normalize(json.loads(example))

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