Home > Software engineering >  How Do I Add a '_' In The Middle of All Data in the Column of a DataFrame
How Do I Add a '_' In The Middle of All Data in the Column of a DataFrame

Time:11-04

eg . I have data like 2008Q1 , 2008Q2 , 2009Q1 in a single column. I want to give output as 2008_Q1 ,2008_Q2

df['quarter'] = df[:4]   '_'   df[2:]

I have tried this but it did not work.

CodePudding user response:

We can try using str.replace here. Assuming data in the column would always have the format 2022Q1:

df["quarter"] = df["quarter"].str.replace(r'(?=Q\d$)', '_', regex=True)

CodePudding user response:

You can try this :

df['quarter'] = df.quarter.apply(lambda x: x[:4]   '_'   x[-2:])
  • Related