Entertainment 001
Entertainment 001

Reputation: 1

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

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.

Upvotes: 0

Views: 42

Answers (2)

Tim Biegeleisen
Tim Biegeleisen

Reputation: 522254

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)

Upvotes: 2

LCMa
LCMa

Reputation: 455

You can try this :

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

Upvotes: 1

Related Questions