I'm working with a column in a pandas dataframe that I would like to flatten into columns. The issue is that some columns are single dictionaries and others are lists of dictionaries.
So for instance the array in a cell in the column may look like this:
{'a': 'x', 'b': 'y', 'c': 'z'}
or like:
[{'a': 'x', 'b': 'y', 'c': 'z'}, {'a': 'd', 'b': 'e', 'c': 'f'}]
I've been playing around with the standard json_normalize function but the output can't seem to handle there being two different types of array:
json_flattened = pd.io.json.json_normalize(df.json_cell.apply(json.loads))
This gives me AttributeError: 'list' object has no attribute 'items'
What I'm hoping to get out the other end is a dataframe that will end up with as many columns as there are repeated elements in the array.
.-------.---.---.---.-----.-----.------.
| index | a | b | c | a_1 | b_1 | c_1 |
:-------+---+---+---+-----+-----+------:
| 0 | x | y | z | NaN | NaN | NaN |
:-------+---+---+---+-----+-----+------:
| 1 | x | y | z | d | e | f |
'-------'---'---'---'-----'-----'------'