Home > Software design >  Create a row for each record separated by some pattern
Create a row for each record separated by some pattern

Time:12-28

I need to create a row in the pandas dataframe for each record in the "ativos" column and the other data ("nome") must be repeated. Records are separated by this string "\r\n" in the "ativos" column. However, I don't know how to do it correctly.

   nome            activos
0  Luiz  ABC\r\nDEF\r\nGHI

Expected result:

   nome activos
0  Luiz     ABC
1  Luiz     DEF
2  Luiz     GHI

CodePudding user response:

Use str.split and explode:

out = df.assign(activos=df['ativos'].str.split(r'\\r\\n')).explode('ativos')
print(out)

# Output
   nome ativos
0  Luiz    ABC
0  Luiz    DEF
0  Luiz    GHI

Setup:

df = pd.DataFrame({'nome': ['Luiz'], 'ativos': [r'ABC\r\nDEF\r\nGHI']})
print(df)

# Output
   nome             ativos
0  Luiz  ABC\r\nDEF\r\nGHI

CodePudding user response:

You can use split and explode:

df['activos'] = df['activos'].str.split(r'\\r\\n')
df.explode('activos', ignore_index=True)
  • Related