在 Pandas 中使用 str.lower()
处理 pandas DataFrame 时,常将 str.lower() 应用于整列字符串,以统一文本格式。我们来看一个示例来了解其工作方式。
下面是一个表示不同水果价格的示例数据集:
import pandas as pd
# Create a DataFrame
fruit_price = pd.DataFrame({
'name': [
'Apple', 'Banana', 'Orange', 'Watermelon',
'Plum', 'Blueberries', 'Dragonfruit', 'Kiwi'
],
'price_usd': [0.88, 0.23, 0.68, 3.98, 0.96, 5.16, 5.27, 1.12]
})
name price_usd
0 Apple 0.88
1 Banana 0.23
2 Orange 0.68
3 Watermelon 3.98
4 Plum 0.96
5 Blueberries 5.16
6 Dragonfruit 5.27
7 Kiwi 1.12
我们可以对 name 列应用 str.lower() 方法,将所有条目转换为小写。操作如下:
fruit_price['name'] = fruit_price['name'].str.lower()
print(fruit_price)
输出如下:
name price_usd
0 apple 0.88
1 banana 0.23
2 orange 0.68
3 watermelon 3.98
4 plum 0.96
5 blueberries 5.16
6 dragonfruit 5.27
7 kiwi 1.12
可以看到,name 列中的所有条目现在都已统一为小写。
交互示例:在电影数据集中更改大小写
我们再来看一个示例。设想我们正在处理一个电影数据集。数据集的每一行代表一部电影,包含标题(movie_title)、类型(movie_genre)和时长(movie_length)三列。下面是初始数据集:
import pandas as pd
# Create a DataFrame
movies = pd.DataFrame({
'movie_title': [
'The Sumif All Fears', 'The Seaborn Identity', 'The Matrices',
"There's Something About Merging", 'Mamma Median!',
'Harry Plotter', 'Kung Fu Pandas', 'While You Were Sorting',
'10 Things I Hate About UNIX'
],
'movie_genre': [
'Action', 'Action', 'Action', 'Comedy', 'Comedy',
'Kids', 'Kids', 'Romance', 'Romance'
],
'movie_length': [124, 119, 136, 119, 108, 159, 92, 103, 97]
})
我们希望将 movie_title 列中的文本统一为小写。可以使用 .str.lower() 方法实现:
# Change movie_title column to lowercase
movies['movie_title'] = movies['movie_title'].str.lower()
# Look at the change
print(movies)
运行上述代码后,会得到如下结果:
movie_title movie_genre movie_length
0 the sumif all fears Action 124
1 the seaborn identity Action 119
2 the matrices Action 136
3 there's something about merging Comedy 119
4 mamma median! Comedy 108
5 harry plotter Kids 159
6 kung fu pandas Kids 92
7 while you were sorting Romance 103
8 10 things i hate about unix Romance 97
通过应用 .str.lower(),我们确保 movie_title 列实现了统一格式化,这对于筛选、比较或执行不区分大小写的操作非常有用。
如需进一步了解如何在 Python 中整合 pandas DataFrame,请观看我们课程 Python for Spreadsheet Users 中的这个视频。
本文内容摘自 Chris Cardillo 在 DataCamp 开设的 Python for Spreadsheet Users 课程。
Python 小写转换常见问答
What is lowercase in Python?
当字符串中的所有字母都是小写时,就是小写形式。与小写相对的是大写形式,即字符串中的所有字母都是大写。否则,如果一个字符串同时包含小写和大写字母,则称为混合大小写。
How to make a string lowercase in Python?
对字符串或其对应变量使用 Python 内置的字符串方法 lower()。例如,'PythoN'.lower() 将返回 'python'。此方法不接受任何参数。
How to make all the strings in a Python list lowercase?
使用列表推导式。例如,[x.lower() for x in ['Python', 'R', 'SQL']] 将返回 ['python', 'r', 'sql']。需要注意的是,列表中的所有项必须是字符串类型,否则程序会报错。
How to make all the strings in a Python Series lowercase?
也可以对 Series 使用列表推导式([x.lower() for x in pd.Series(['Python', 'R', 'SQL'])]),但更好的做法是使用 Series.str.lower() 方法。例如,pd.Series(['Python', 'R', 'SQL']).str.lower() 将返回:
0 python
1 r
2 sql
dtype: object
需要注意的是,Series 对象的所有值必须为字符串类型,且不应包含缺失值,否则程序会报错。
How to check if all the letters in a Python string are lowercase?
对字符串或其对应变量使用 Python 内置的字符串方法 islower()。例如,'Python'.islower() 将返回 False,因为字符串中有一个大写字母;而 'python'.islower() 将返回 True。
What type of data do the lower() and islower() methods return?
lower() 方法返回字符串,而 islower() 方法返回布尔值。
Do the lower() and islower() methods work on numbers?
不会。将这些方法应用于数字(例如 1.lower())或任何非字符串的数据类型都会导致报错。
What happens if we apply the lower() or islower() methods on a string containing digits, white spaces, or special characters?
lower() 方法会保持这类字符不变,islower() 会忽略它们。例如,'Python1'.lower() 将返回 'python1','Python & R'.lower() 将返回 'python & r',而 'python & r'.islower() 将返回 True。对于包含表情符号的字符串同样适用:'I ❤️ Python'.lower() 将返回 'i ❤️ python'。
What are the opposite methods to lower() and islower() to make a string uppercase?
upper() 和 isupper()。这些方法的语法和用法与 lower() 和 islower() 相同,也仅适用于字符串数据类型,并会忽略字符串中的数字、空格或特殊字符。要将 Python Series 中的所有字符串转换为大写,请使用 Series.str.upper() 方法。
.lower()是一个内置的 Python 字符串方法,主要用于处理字符串。.lower()方法不接受任何参数,会将给定字符串中的每个大写字符转换为小写并返回转换后的小写字符串。如果给定字符串中没有大写字符,则返回原字符串。在 pandas 的语境中,
.str.lower()是一个用于操作 pandas Series(如 DataFrame 中的列)的专用函数。它会将.lower()的功能应用到 Series 中的每个字符串元素,是规范化文本数据的必备工具。通过这个动手练习在 Python 中实践大小写转换。