I accidentally closed this question with a link to the wrong duplicate. Here is the correct one: Pandas split column of lists into multiple columns.
Suppose I have a dataframe of which one column is a list (of a known and identical length) or tuple, for example:
df1 = pd.DataFrame(
{'vals': [['a', 'b', 'c', 'd'],['e','f','g','h']]}
)
ie:
vals
0 [a, b, c, d]
1 [e, f, g, h]
I want to extra the values in "vals" into separate named columns. I can do this clumsily by iterating through the rows:
for i in range(df1.shape[0]):
for j in range(0,4):
df1.loc[i, 'vals_'+j] = df1.loc[i, 'vals'] [j]
Result as desired:
vals vals_0 vals_1 vals_2 vals_3
0 [a, b, c, d] a b c d
1 [e, f, g, h] e f g h
Is there a neater (vectorised) way? I tried using [] but I get an error.
for j in range (0,4)
df1['vals_'+str(j)] = df1['vals'][j]
gives:
ValueError: Length of values does not match length of index
It looks like Pandas is trying to apply the [] operator to the series/dataframe rather than the column content.