Given a series of unknown size inner list:
import pandas as pd
sr = pd.Series([['a', 'b', 'c', 'b'], ['a', 'a', 'd'], ['b']])
[out]:
0 [a, b, c, b]
1 [a, a, d]
2 [b]
The goal is to use values in the inner list to create the columns and populate its value with the count of the items in each row, i.e.
a b c d
0 1.0 2.0 1.0 NaN
1 2.0 NaN NaN 1.0
2 NaN 1.0 NaN NaN
I have tried achieving the the above by iterating through each row and converting them into Counter
objects and recreating the dataframe using the list of counter dictionaries:
>>> from collections import Counter
>>> pd.DataFrame([dict(Counter(row)) for row in pd.Series([['a', 'b', 'c', 'b'], ['a', 'a', 'd'], ['b']])])
Is there a simpler way to do this? Perhaps with .pivot()
?