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

Check if last character in series is upper case and convert to lowercase if true

I have a column of strings that look similar to the following:

1          IX-1-a
2          IX-1-b
3          IX-1-C
4          IX-1-D

Some end in lowercase letters while others end in uppercase. I need to standardize all endings to lowercase without affecting the letters at the beginning of the string. Below is some code fragment that I am working with to make changes within the series but it doesn’t quite work.

if i in tw4515['Unnamed: 0'].str[-1].str.isupper() == True:
      tw4515['Unnamed: 0'].str[-1].str.lower()

How can the truth table from tw4515['Unnamed: 0'].str[-1].str.isupper() be utilized efficiently to affect conditional changes?

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

>Solution :

One option is to split once from the right side, make the second part lowercase, then combine:

tmp = s.str.rsplit('-', 1)
out = tmp.str[0] + '-' + tmp.str[1].str.lower()

If the last part is always a single letter, @Barmar’s solution is even better:

out = s.str[:-1] + s.str[-1].str.lower()

Output:

1    IX-1-a
2    IX-1-b
3    IX-1-c
4    IX-1-d
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