please help..
file.json
[
{"fullname": "mona", "phones": [{"phone": "21323131"}], "areas": [{"area": "Texas"}, {"area": "New York"}] },
{"fullname": "joni", "phones": [{"phone": "546465464"},{"phone": "45345353"}], "areas": [{"area": "California"},{"area": "San Jose"}] }
]
i have dataframe like this
import pandas as pd
df = pd.read_json('file.json')
print(df.head(2))
Output:
fullname phones Areas
mona [{'phone': '21323131'}] [{'area': 'Texas'}, {'area': 'New York'}]
joni [{'phone': '546465464'},{'phone': '45345353'}] [{'area': 'California'},{'area': 'San Jose'}]
how to extract dataframe to be like this?
fullname phone Areas
mona 21323131 Texas, New York
joni 546465464 California, San Jose
joni 45345353 California, San Jose
Let's try explode
along with Series.str.get
:
s = df['areas'].explode().str.get('area').groupby(level=0).agg(', '.join)
d = df.explode('phones').assign(areas=s, phones=lambda x: x['phones'].str.get('phone'))
print(d)
fullname phones areas
0 mona 21323131 Texas, New York
1 joni 546465464 California, San Jose
1 joni 45345353 California, San Jose
Here is a possible solution:
import json
import pandas as pd
with open('file.json') as f:
data = json.load(f)
exploded_data = [{'fullname': x['fullname'],
'phone': p['phone'],
'area': a['area']}
for x in data for p, a in zip(x['phones'], x['areas'])]
df = pd.DataFrame(exploded_data)
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.