Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Flip order of characters within a string using Python

I wish to flip the order of characters within the ‘date’ column using Python

Data

id  type  date
aa  hi    2022 Q1
aa  hi    2022 Q2
    

Desired

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

id  type    date
aa  hi      Q1 2022
aa  hi      Q2 2022

Doing

I believe I can separate and then reverse them?

a = df.split()

Any suggestion is helpful

>Solution :

Indeed, I would do it with list comprehension:

df = pd.DataFrame({'date':['2022 Q1','2022 Q2']})
df['date'] = [' '.join(x.split()[::-1]) for x in df['date']]

Using print(df['date']) Outputs:

0    Q1 2022
1    Q2 2022
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading