I have json data which compiled by attributes(column) and its elements(data) for each.
[
{
"attribute_id": "a1",
"attribute_name": "Profailkir Idkir",
"elements":[
{
"formValues": [
"0000a634940"
],
"id": "0000a634940"
},
{
"formValues": [
"0000c84724"
],
"id": "0000c84724"
}]
},
{
"attribute_id": "a2",
"attribute_name": "Profailkir Nama",
"elements":[
{
"formValues": [
"'AFIFAH BINTI KHAIRUL JUBRI"
],
"id": "'AFIFAH BINTI KHAIRUL JUBRI"
},
{
"formValues": [
"'AINUN JARIAH BINTI HASHIM"
],
"id": "'AINUN JARIAH BINTI HASHIM"
}]
}
]
but when i tried using
import pandas as pd
df = pd.DataFrame(data)
print(df)
the result is become appended only, not extracting the formValues.
How can I convert into Dataframe in below reading format?
Profailkir Idkir | Profailkir Nama | |
---|---|---|
0 | 0000a634940 | ‘AFIFAH BINTI KHAIRUL JUBRI |
1 | 0000c84724 | ‘AINUN JARIAH BINTI HASHIM |
2
Answers
Assuming you want the
id
values from each element, you could use a dictionary comprehension on your data to reshape it:Output:
If you want the
formValues
(and they are of the same length for each attribute), you can modify the code slightly:For your sample data, the result is the same.
Try it this way, I hope I got it right