为了账号安全,请及时绑定邮箱和手机立即绑定

遍历行以取消大写字符串单词

遍历行以取消大写字符串单词

PIPIONE 2023-05-23 10:13:44
我想知道如何转换此列中每个单词的首字母:TestThere is a cat UNDER the table The pen is working WELL.变成小写,为了有Test    there is a cat uNDER the table     the pen is working wELL.对于字符串,可以使用以下代码:" ".join(i[0].lower()+i[1:] for i in line.split(" "))我如何通过列中的行迭代它?
查看完整描述

3 回答

?
蝴蝶刀刀

TA贡献1801条经验 获得超8个赞

Series.str.replace与正则表达式模式和替换 lambda 函数一起使用。您可以测试正则表达式模式here

df['Test'] = df['Test'].str.replace(r'((?<=\b)\S)', lambda x: x.group(1).lower())

结果:

                             Test

0  there is a cat uNDER the table

1        the pen is working wELL.


查看完整回答
反对 回复 2023-05-23
?
慕斯709654

TA贡献1840条经验 获得超5个赞

构建您的解决方案:


df["Test"] = [" ".join(entry[0].lower() + entry[1:] 

              for entry in word.split()) 

              for word in df.Test]



df


            Test

0   there is a cat uNDER the table

1   the pen is working wELL.


查看完整回答
反对 回复 2023-05-23
?
互换的青春

TA贡献1797条经验 获得超6个赞

以下


import pandas as pd


def uncapitalise(x):

  words = x.split(' ')

  result = []

  for word in words:

    print(word)

    if word:

      word = word[0].lower() + word[1:]

    result.append(word)

  return ' '.join(result)


data = ['Test','There is a cat UNDER the table ','The pen is working WELL.']

df = pd.DataFrame(data)

df[0] = df[0].apply(uncapitalise)

print(df)


查看完整回答
反对 回复 2023-05-23
  • 3 回答
  • 0 关注
  • 99 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信