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

Time:04-15

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?

CodePudding user response:

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
  • Related