My pandas DataFrame looks like this:
id | address |
---|---|
1 | [{'city': 'MURFREESBORO', 'line': ['9999 Candy Cane Island'], 'postalCode': '39999', 'state': '56'}] |
2 | [{'city': 'LIKELAND', 'line': ['11111 WS 80RD ST'], 'postalCode': '71398', 'state': '99'}] |
3 | [{'city': 'CHASS', 'line': ['36 LONDON LN'], 'postalCode': '269235', 'state': '35'}] |
How do I convert this column into multiple columns to look like this?
id | city | line | postalcode | state |
---|---|---|---|---|
1 | MURFREESBORO | 9999 Candy Cane Island | 39999 | 56 |
2 | LIKELAND | 11111 WS 80RD ST | 71398 | 99 |
3 | CHASS | 36 LONDON LN | 269235 | 35 |
I have tried multiple different ways:
df = pd.json_normalize(newdf['address'])
# AND
newdf['address'] = newdf['address'].apply(lambda x: "'" + str(x) + "'")
newdf['address'] = newdf['address'].apply(str).str.replace('[', '').str.replace(']', '')
Data to construct the DataFrame:
{'id': [1, 2, 3],
'address': [[{'city': 'MURFREESBORO', 'line': ['9999 Candy Cane Island'],
'postalCode': '39999', 'state': '56'}],
[{'city': 'LIKELAND', 'line': ['11111 WS 80RD ST'],
'postalCode': '71398', 'state': '99'}],
[{'city': 'CHASS','line': ['36 LONDON LN'],
'postalCode': '269235', 'state': '35'}]]}