Python - 从 Pandas DataFrame 中删除包含数字的行
Python - Drop rows from a Pandas DataFrame that contain numbers
我有一个包含这样一列的数据框:
Value
xyz123
123
abc
def
我想删除所有包含数字的行,所以我最终得到这样的数据框:
Value
abc
def
我试过了
df = df[df['Value'].str.contains(r'[^a-z]')]
但是我得到了这个错误:
"TypeError: 'method' object is not subscriptable"
独立于变量命名问题,您可以更明确地只删除带数字的行:
df[~df.Value.str.contains(r'\d')]
Value
2 abc
3 def
\d
:
Matches any Unicode decimal digit (that is, any character in Unicode
character category [Nd]). This includes [0-9], and also many other
digit characters. If the ASCII flag is used only [0-9] is matched (but
the flag affects the entire regular expression, so in such cases using
an explicit [0-9] may be a better choice).
IIUCisalpha
df[df.Value.str.isalpha()]
Out[2057]:
Value
2 abc
3 def
我有一个包含这样一列的数据框:
Value
xyz123
123
abc
def
我想删除所有包含数字的行,所以我最终得到这样的数据框:
Value
abc
def
我试过了
df = df[df['Value'].str.contains(r'[^a-z]')]
但是我得到了这个错误:
"TypeError: 'method' object is not subscriptable"
独立于变量命名问题,您可以更明确地只删除带数字的行:
df[~df.Value.str.contains(r'\d')]
Value
2 abc
3 def
\d
:
Matches any Unicode decimal digit (that is, any character in Unicode character category [Nd]). This includes [0-9], and also many other digit characters. If the ASCII flag is used only [0-9] is matched (but the flag affects the entire regular expression, so in such cases using an explicit [0-9] may be a better choice).
IIUCisalpha
df[df.Value.str.isalpha()]
Out[2057]:
Value
2 abc
3 def