Python dataframe list-column splitting
Solution 1:
Use pd.DataFrame.from_records
and join
:
cols = ['MatchWord', 'Prox', 'MatchID']
out = df.join(pd.DataFrame.from_records(df['Ref'], index=df.index, columns=cols))
print(out)
# Output
RefID Ref MatchWord Prox MatchID
0 Ref1 (baby, 60, 0) baby 60 0
1 Ref2 (something, 90, 2) something 90 2
Solution 2:
You can apply(pd.Series)
. This will unpack the items as columns:
df['Ref'].apply(pd.Series, index=['MatchWord', 'Prox', 'MatchID'])
Or use the DataFrame
constructor:
pd.DataFrame(df['Ref'].to_list(), columns=['MatchWord', 'Prox', 'MatchID'])
Output:
MatchWord Prox MatchID
0 baby 60 0
1 something 90 2