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

我想知道如何转换此列中每个单词的首字母:


Test

There 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(" "))

我如何通过列中的行迭代它?


PIPIONE
浏览 140回答 3
3回答

蝴蝶刀刀

Series.str.replace与正则表达式模式和替换 lambda 函数一起使用。您可以测试正则表达式模式here:df['Test'] = df['Test'].str.replace(r'((?<=\b)\S)', lambda x: x.group(1).lower())结果:                             Test0  there is a cat uNDER the table1        the pen is working wELL.

慕斯709654

构建您的解决方案:df["Test"] = [" ".join(entry[0].lower() + entry[1:]&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for entry in word.split())&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for word in df.Test]df&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Test0&nbsp; &nbsp;there is a cat uNDER the table1&nbsp; &nbsp;the pen is working wELL.

互换的青春

以下import pandas as pddef uncapitalise(x):&nbsp; words = x.split(' ')&nbsp; result = []&nbsp; for word in words:&nbsp; &nbsp; print(word)&nbsp; &nbsp; if word:&nbsp; &nbsp; &nbsp; word = word[0].lower() + word[1:]&nbsp; &nbsp; result.append(word)&nbsp; 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)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python