Unable to produce specific group & subgroup in JSON using Python
I am trying to generate nested JSON using Python Pandas. But, somehow not able to figure it out as how the sub-grouping works or rather I can generate it. Not sure how to pack the subgroup first & then going for the group.
Is there any built-in function in python or pandas or any relevant package of Python that can do the same without writing lots of code?
What I have written is below:
j = (df.groupby(['empno', 'work_id'], as_index=False)
.apply(lambda x: x[['status_id', 'type', 'languageId', 'language',
'email', 'game_name', 'experience_level', 'CellNo'
]].to_dict('r'))
.reset_index()
.rename(columns={0: 'workPostDetails'})
.to_json(orient='records'))
print("JSON::")
print(j)
Sample Data:
empno work_id status_id type languageId Language send_by recived_by game_name experience_level
----- ---------- ---------- ----- ----------- --------- --------- ---------- ------------- -----------------
0017 X123 2101 email 1 All a@abc.com b@xyz.com C++ Expert
0017 X123 2103 phone 1 All +1 9282828282 +1 9383838383 A++ Intermediate
Expected JSON:
{
"empno": "0017",
"work_id": "X123",
"workPostDetails": {
"workDetails": [
{
"status_id": "2101",
"type": "email",
"languageId": "1",
"language": "All-Read-Write",
"send_by": {
"email": "a@abc.com"
},
"recived_by": [
{
"email": "b@xyz.com"
}
],
"skillDetails": [
{
"game_name": "EA Sports",
"experience_level": "Expert"
}
]
},
{
"status_id": "2103",
"type": "sms",
"languageId": "2",
"language": "All-Read",
"send_by": {
"CellNo": "+1 9282828282"
},
"recived_by": [
{
"CellNo": "+1 9383838383"
}
],
"skillDetails": [
{
"game_name": "Candy Crush",
"experience_level": "Intermediate"
}
]
}
]
}
}
You could first prepare your dataframe by adapting the columns, and then iterate the GroupBy object. It would give:
# act on a copy to preserve original data
df2 = df.copy()
# prepare columns
df2[['send_by', 'recived_by']] = df[['send_by', 'recived_by']].apply(
lambda x: x.apply(lambda y: { 'email': y} if '@' in y else {'CellNo':y}))
df2['skillDetails'] = df2.apply(lambda x: {k: x[k]
for k in ('game_name','experience_level')}, axis=1)
df2.drop(columns=['game_name','experience_level'], inplace=True)
# generate the json string
j = pandas.DataFrame(((name[0], name[1],
val.loc(axis=1)['status_id':].to_dict('r'))
for name, val in df2.groupby(['empno', 'work_id'])),
columns = ['empno', 'work_id', 'workPostDetail']).to_json(orient ='records')
Here is the final version -
df2 = df.copy()
# prepare columns
df2[['send_by', 'recived_by']] = df[['send_by', 'recived_by']].apply(
lambda x: x.apply(lambda y: { 'email': y} if '@' in y else {'CellNo':y}))
df2['skillDetails'] = df2.apply(lambda x: {k: x[k]
for k in ('game_name','experience_level')}, axis=1)
df2.drop(columns=['game_name','experience_level'], inplace=True)
df2['workDetails'] = df2.apply(lambda x1: {k1: x1[k1]
for k1 in
('status_id','type','languageId','Language','send_by','recived_by','skillDetails')}, axis=1)
df2.drop(columns=['status_id','type','languageId', 'Language', 'send_by', 'recived_by', 'skillDetails'], inplace=True)
# generate the json string
j = p.DataFrame(((name[0], name[1],
val.loc(axis=1)['workDetails':].to_dict('r'))
for name, val in df2.groupby(['empno', 'work_id'])),
columns = ['empno', 'work_id', 'workPostDetail']).to_json(orient ='records')
Thanks guys!
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.