Home > Back-end >  Splitting one row into two based on condition
Splitting one row into two based on condition

Time:07-09

I have a situation like you can see in the image: Here

Basically, I need to duplicate the rows where are 2 numbers splitted with ' ' and then from first row remove part of the number after ' '. For second row do the opposite, remove part of the number before ' '. I need to do that for every occurrence of values with ' ' in that column.

To make it clear, i need to split that one row with ' ' in number into something like this:

02.05.2022 0 221350 Michaelis F Lager

02.05.2022 0 221181 Michaelis F lager

Does anyone know how i can achieve this?

CodePudding user response:

you can try like this:

import pandas as pd

df = pd.DataFrame({"Col1": ["221350*66666", 99999,123456], "Col2": ["A", "B", "C"]})

df = df.assign(Col3=df["Col1"].str.split("*")).explode("Col3")
df["Col3"].fillna(df["Col1"], inplace=True)
print(df)
           Col1 Col2    Col3
0  221350*66666    A  221350
0  221350*66666    A   66666
1         99999    B   99999
2        123456    C  123456
  • Related