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

python – create new column based on last two letters of a string

I have a large dataframe containing many thousand URNs each with a country code on the end. I would like to create a new column that isolates those country codes:

my df:

    urn
0   CS16-1533232-GB 
1   CS16-1533233-GB     
2   CS16-1533234-GB 
3   CS16-1533235-BZ 
4   CS16-1533238-GB

Desired output:

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

    urn              country
0   CS16-1533232-GB   GB
1   CS16-1533233-GB   GB
2   CS16-1533234-GB   GB
3   CS16-1533235-BZ   BZ
4   CS16-1533238-GB   GB

>Solution :

If you always have 2 letters, simply slice:

df['country'] = df['urn'].str[-2:]

Else, extract the last letters with str.extract:

df['country'] = df['urn'].str.extract(r'([A-Z]+)$', expand=False)
# or non "-"
df['country'] = df['urn'].str.extract(r'([^-]+)$', expand=False)

Output:

               urn country
0  CS16-1533232-GB      GB
1  CS16-1533233-GB      GB
2  CS16-1533234-GB      GB
3  CS16-1533235-BZ      BZ
4  CS16-1533238-GB      GB
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