Reputation: 20342
I have a data frame that looks like this.
import pandas as pd
# Intitialise data of lists
data1 = [{'eno_id': '079490-2|0799-2', 'ID':'27808'},
{'eno_id': '1707-2', 'ID':'2019', 'ID':'18945'},
{'eno_id': '5045-1|5045-21', 'ID':'2020', 'ID':'36618'}]
df1 = pd.DataFrame(data1)
df1
How can I split out, or expand, the column with pipe characters, so it looks like this?
data2 = [{'eno_id': '079490-2', 'ID':'27808'},
{'eno_id': '0799-2', 'ID':'27808'},
{'eno_id': '1707-2', 'ID':'18945'},
{'eno_id': '5045-1', 'ID':'36618'},
{'eno_id': '5045-21', 'ID':'36618'}]
df2 = pd.DataFrame(data2)
df2
Upvotes: 1
Views: 1037
Reputation: 9207
Split it like this and then use .explode():
df1['eno_id'] = df1['eno_id'].str.split('|')
df1 = df1.explode('eno_id')
Upvotes: 2