I have a column in a DataFrame
that contains JSON strings, each representing a list of dictionaries:
id Number Type Class Name datiles
0 292 C 1 2 A [{"did":{"id":"3","num":1},"NId":"a1,b1,c1","Att":null,"isnull":false,"number":"M90","label":[{"title":"Dear","Info":{"Id":null,"id2":2,"Name":"x"}},{"title":"Dear","Info":{"Id":null,"id2":2,"Name":"x"}}],"codes":[],"rule":null}]
1 293 C 1 2 A [{"did":{"id":"3","num":1},"NId":"a1,b1,c1","Att":null,"isnull":false,"number":"M90","label":[{"title":"Dear","Info":{"Id":null,"id2":2,"Name":"x"}},{"title":"Dear","Info":{"Id":null,"id2":2,"Name":"x"}}],"codes":[],"rule":null}]
I want to convert each row in datiles column to rows and columns and join them with the original data frame as shown on the sample below:
id Number Type Class Name did NId Att ..... .... label ........
0292 C 1 2 A {"id":"3","num":1} a1,b1,c1 null [{"title":"Dear","Info"{"Id":null,"id2":2,"Name":"x"}},{"title":"Dear","Info":{"Id":null,"id2":2,"Name":"x"}}]
I have done this as I need but I don't know how to join it with the original data frame since I don't have a key between them:
df['datiles']=data['datiles'].apply(json.loads)
df2 = pd.DataFrame([])
for x in df['datiles'].values.tolist():
df2 = df2 .append(pd.DataFrame(x))
display(df2)
How can I split the column and join at the same time? I have tried to use json_normalize but I get this error
AttributeError: 'list' object has no attribute 'values'
Also, I have seen those posts but does not work, may that because of the list structure
How to convert python JSON rows to dataframe columns without looping
Pandas split column of lists into multiple columns
How to split a list of dictionaries into multiple columns keeping the same index?