Home > OS >  How to split a dataframe column into two columns and transform values in one expression using Python
How to split a dataframe column into two columns and transform values in one expression using Python

Time:04-17

I need to split a column containing strings into two columns which in this particular case I could do like

df[['col1','col2']] = df['col1'].str.split('-', expand=True).

But I also need to apply a transformation to the second string before storing it in col2 and this transformation depends on some property of the value in col1. Let's for example say that the string in col2 should be reversed if the length of the string in col1 is 5.

Is this possible to do this expanding the above expression in some way?

CodePudding user response:

As pointed out in @Ynjxsjmh's comment you can use .assign(), but you'd need a lambda function to give you access to the current state of the dataframe (you need access to both new columns):

df = pd.DataFrame({"Col1": ["123-abc"] * 3   ["12345-abcde"] * 3})

df[["Col1", "Col2"]] = (
    df["Col1"]
    .str.split("-", expand=True)
    .rename(columns={0: "C1", 1: "C2"})
    .assign(C2=lambda df: df["C2"].where(df["C1"].str.len().ne(5), df["C2"].str[::-1]))
)
  • Related