The Pandas 中的 query() 方法允许您过滤数据帧,并且当与字符串方法结合使用时,可能性变得广泛。
在本教程中,您将学习如何在query()
功能来细化和过滤数据。
检查子字符串:str.contains()
The str.contains()
方法检查字符串中是否存在子字符串并返回布尔系列。
让我们深入研究一个例子:
import pandas as pd
data = {
'Names': ['Alice Cooper', 'BOB', 'charlie', '123Dylan', 'Elle45', 'FRANK ', ' Georgia ', 'Henry-White', 'isabelle99', 'J123ack', ' karen ', 'LOL123'],
'Codes': ['A1B2', 'XYZ', 'zyx998', '1234', 'W5W5', ' ', 'AB.CD', '99.99', 'HELLO', 'world', 'space ', '123.45']
}
df = pd.DataFrame(data)
result = df.query('Names.str.contains("Dylan")')
print(result)
Output:
Names Codes
3 123Dylan 1234
当使用query()
方法结合str.contains()
,我们有效地过滤掉了包含子字符串“Dylan”的行。
str.startswith() 和 str.endswith()
您可以使用str.startswith()
and str.endswith()
方法根据 DataFrame 中的某些字符串是否以特定子字符串开头或结尾来过滤行。
Using str.startswith()
您可以使用str.startswith()
如果要过滤字符串以特定子字符串开头的行,请使用以下方法:
# Filtering names that start with 'ch'
result_startswith = df.query('Names.str.startswith("ch")')
print(result_startswith)
Output:
Names Codes
2 charlie zyx998
在此代码中,您要搜索以“ch”开头的名称。结果,“charlie”被返回。
Using str.endswith()
您可以使用str.endswith()
如果要过滤字符串以特定子字符串结尾的行,请使用以下方法:
# Filtering codes that end with '5'
result_endswith = df.query('Codes.str.endswith("5")')
print(result_endswith)
Output:
Names Codes
4 Elle45 W5W5
11 LOL123 123.45
在这里,我们得到代码以“5”结尾的行。
检查大写:str.isupper()
The str.isupper()
方法允许您检查特定字符串是否完全大写。
# Filtering rows where 'Names' is entirely in uppercase
result_uppercase = df.query('Names.str.isupper()')
print(result_uppercase[['Names']])
Output:
Names
1 BOB
5 FRANK
11 LOL123
根据给定的代码,目标是过滤“Names”列完全由大写字符组成的行。
输出显示了“BOB”、“FRANK”和“LOL123”等条目——所有这些都是完全大写的。
检查小写:str.islower()
The str.islower()
检查特定字符串是否完全小写:
# Filtering rows where 'Names' is entirely in lowercase
result_lowercase = df.query('Names.str.islower()')
print(result_lowercase[['Names']])
Output:
Names
2 charlie
8 isabelle99
10 karen
输出突出显示“charlie”、“isabelle99”和“karen”等条目 - 所有这些都是小写的。
检查数字字符
The str.isnumeric()
and str.isdigit()
.methods 允许您检查字符串是否包含纯数字字符。
这两种方法都用于识别仅由数字组成的字符串,但它们的行为存在细微差别,特别是在考虑 Unicode 字符时。
Using str.isnumeric()
此方法检查字符串中的所有字符是否都是数字字符并且字符串不为空:
import pandas as pd
df = pd.DataFrame({
'data': ['12345', '½', 'abcd', '123a']
})
# Query with str.isnumeric()
print(df.query("`data`.str.isnumeric()"))
Output:
data
0 12345
1 ½
Using str.isdigit()
The str.isnumeric()
,方法检查字符串中的所有字符是否都是数字并且字符串不为空:
# Query with str.isdigit()
print(df.query("`data`.str.isdigit()"))
Output:
data
0 12345
正如我们所看到的,str.isnumeric()
将“12345”和“½”识别为包含数字字符,而str.isdigit()
仅将“12345”识别为包含数字字符。
请注意在列名称周围使用反引号 (“),当列名称与函数名称冲突或包含空格或特殊字符时需要使用反引号 (“)。
检查字母数字:str.isalnum()
The str.isalnum()
Pandas 中的方法允许您检查字符串是否由字母数字字符(字母和数字)组成,不包含任何空格或特殊字符。
# Filtering rows where 'Names' contains only alphanumeric characters
result_alnum = df.query('Names.str.isalnum()')
print(result_alnum[['Names']])
Output:
Names
1 BOB
2 charlie
3 123Dylan
4 Elle45
8 isabelle99
9 J123ack
11 LOL123
从执行的代码中,您可以观察到我们正在尝试过滤“名称”列仅由字母数字字符组成的行。
检查小数:str.isdecimal()
The str.isdecimal()
检查字符串是否包含 Unicode 十进制类别中的字符,该类别本质上包括整数,但不包括浮点数或任何其他数字表示形式。
# Filtering rows where 'Names' is purely decimal
result_decimal = df.query('Codes.str.isdecimal()')
print(result_decimal[['Codes']])
Output:
Codes
3 1234
从上面的代码中,我们过滤了“代码”列完全由十进制字符组成的行。在我们的示例数据中,唯一的输出是“1234”。
检查空格:str.isspace()
识别完全由空格组成的字符串对于数据清理和验证过程至关重要。
此类字符串通常可能是数据输入错误、导入未对齐或占位符值造成的。使用str.isspace()
Pandas 中的方法,您可以毫不费力地检测这些仅包含空格的字符串。
让我们用示例 DataFrame 来说明它的用法:
# Filtering rows where 'Names' is entirely whitespace
result_whitespace = df.query('Codes.str.isspace()')
print(result_whitespace[['Names']])
Output:
Names
5 FRANK
从上面的代码中,我们过滤了“Codes”列仅包含空格的行。
然后我们打印该代码的名称“FRANK”。
检查标题大小写:str.istitle()
标题大小写(每个单词的第一个字母大写)是名称、标题或标题的常见格式。
您可以使用str.istitle()
方法来检查遵循此模式的字符串:
# Filtering rows where 'Names' is in titlecase
result_titlecase = df.query('Names.str.istitle()')
print(result_titlecase[['Names']])
Output:
Names
0 Alice Cooper
3 123Dylan
4 Elle45
6 Georgia
7 Henry-White
从执行的代码中,我们过滤“名称”列遵循标题大小写格式的行。
输出突出显示“Alice Cooper”和“Henry-White”。尽管“Henry-White”包含连字符,但每个分段单词都以大写字母开头,这与标题结构对齐。