检查串联中的最后一个字符是否为上案,如果是的,则转换为小写

发布于 2025-01-21 12:56:48 字数 408 浏览 0 评论 0原文

我有一列的字符串看起来与以下几个相似:

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

有些以小写字母结尾,而另一些则以大写结尾。我需要标准化所有结尾以小写,而不会在字符串开始时影响字母。以下是我正在使用的一些代码片段,以在该系列中进行更改,但这并不是很可行。

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

tw4515 ['numed:0']的真实表如何有效地利用影响条件更改?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

度的依靠╰つ 2025-01-28 12:56:48

一种选择是从右侧分开一次,使第二部分小写,然后结合:

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

如果最后一部分始终是一个字母,@barmar的解决方案更好的

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

1    IX-1-a
2    IX-1-b
3    IX-1-c
4    IX-1-d

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文