How do I convert the dictionary to df?
data = {'records': [{'centre_contact_no': 1578},
{'centre_contact_no': 7517},
{'centre_contact_no': 3590}
]
}
i have tried
data = pd.DataFrame.from_records(data)
CodePudding user response:
Do you simply want:
df = pd.DataFrame(data['records'])
output:
centre_contact_no
0 1578
1 7517
2 3590
Or with a MultiIndex:
df = pd.concat({k: pd.DataFrame(v) for k,v in data.items()}, axis=1)
output:
records
centre_contact_no
0 1578
1 7517
2 3590
CodePudding user response:
You have added extra }
closing bracket in the dict which might probably not converting it to dataframe
After removing and running your code,
import pandas as pd
data = {'records': [
{'centre_contact_no': 1578},
{'centre_contact_no': 7517},
{'centre_contact_no': 3590}
]
}
data = pd.DataFrame.from_records(data)
print(data)
output of the above code is
records
0 {'centre_contact_no': 1578}
1 {'centre_contact_no': 7517}
2 {'centre_contact_no': 3590}
if you simply want row wise format, instead of data
use data['records']
import pandas as pd
data = {'records': [
{'centre_contact_no': 1578},
{'centre_contact_no': 7517},
{'centre_contact_no': 3590}
]
}
data = pd.DataFrame.from_records(data['records'])
print(data)
the output of the above code is
centre_contact_no
0 1578
1 7517
2 3590
Hope this is what you are looking for, if not please feel free to comment, thanks.