ASH
ASH

Reputation: 20342

How can we split a pipe delimited field in a data frame?

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

enter image description here

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

enter image description here

Upvotes: 1

Views: 1037

Answers (1)

Andreas
Andreas

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

Related Questions