I have the following DataFrame:
Index Letter Numbers
1 A [1, 11]
2 B [2, 22]
3 C [3, 33]
And I want to "expand" the list of numbers just so each number has it's own row, like this:
Index Letter Numbers
1 A 1
2 A 11
3 B 2
4 B 22
5 C 3
6 C 33
I tried to achieve this by using the following code:
import pandas as pd
df = pd.DataFrame({
'Letter': ['A', 'B', 'C'],
'Numbers': [[1, 11], [2, 22], [3, 33]]
})
for i, row in df.iterrows():
for num in row['Numbers']:
new_row = row.copy()
new_row['Numbers'] = num
df = df.append(new_row, ignore_index=True)
df = df.loc[df.apply(lambda x: type(x['Numbers']) != list, axis=1)]
It works for the example, but in a larger DataFrame (a few hundred thousand lines for example) it takes a lot of time. Is there a better, more optimized way to do it? I tried using the apply method, but it clears my DataFrame for some reason...