My original data -
pd.DataFrame([
{'col1': 1, 'col2': 11, 'col3': ['A', 'B'] },
{'col1': 2, 'col2': 22, 'col3': ['P', 'Q', 'R'] },
])
which gives me -
col1 col2 col3
0 1 11 ['A', 'B']
1 2 22 ['P', 'Q', 'R']
I'd like to expand the rows depending on contents of the array in col3
i.e. I'd like to get a dataframe
like this one -
col1 col2 col3
0 1 11 'A'
1 1 11 'B'
2 2 22 'P'
3 2 22 'Q'
4 2 22 'R'
What's a good way accomplishing this? I found a similar question here however it doesn't include col3
in the output.
I also have another similar question here however since it has strings in the column. The same solution wouldn't work here because split()
wouldn't work for list
.