我有一个pandas.DataFrame
如下,我只需删除那些只是数字的行。如何在python中仅使用正则表达式删除那些纯数字的行。
我需要删除那些只有数字,而不是那些有数字+字母的数字。
df
0
0
1
1
01t23
2
02t13
02t14
02t16
02t18
02t21
02t22
02t23
40745
abcd
abcd1
tyu78890
hpsc77888
对于名称为df
的一列的数据框,您可以删除纯数字,如:
df['df'][~df['df'].str.isnumeric()]
df
0
0
1
1
01t23
2
02t13
02t14
02t16
02t18
02t21
02t22
02t23
40745
abcd
abcd1
tyu78890
hpsc77888"""), header=0)
print(df)
print(df['df'][~df['df'].str.isnumeric()])
df
0 0
1 0
2 1
3 1
4 01t23
5 2
6 02t13
7 02t14
8 02t16
9 02t18
10 02t21
11 02t22
12 02t23
13 40745
14 abcd
15 abcd1
16 tyu78890
17 hpsc77888
4 01t23
6 02t13
7 02t14
8 02t16
9 02t18
10 02t21
11 02t22
12 02t23
14 abcd
15 abcd1
16 tyu78890
17 hpsc77888
Name: df, dtype: object
使用contains
by regex ^\d+$
- 字符串开头,1位或更多位数字以及boolean indexing
字符串结尾:
df = df[~df['df'].str.contains('^\d+$')]
df = df[pd.to_numeric(df['df'], errors='coerce').isna()]
print (df)
df
4 01t23
6 02t13
7 02t14
8 02t16
9 02t18
10 02t21
11 02t22
12 02t23
14 abcd
15 abcd1
16 tyu78890
17 hpsc77888
你也可以使用pandas mask()函数,如:
df=pd.DataFrame(["0",
"0",
"1",
"1",
"01t23",
"2",
"02t13",
"02t14",
"02t16",
"02t18",
"02t21",
"02t22",
"02t23",
"40745",
"abcd",
"abcd1",
"tyu78890",
"hpsc77888"],columns=["A"])
df.mask(df["A"].str.isnumeric()).dropna()
A
4 01t23
6 02t13
7 02t14
8 02t16
9 02t18
10 02t21
11 02t22
12 02t23
14 abcd
15 abcd1
16 tyu78890
17 hpsc77888
要么:
notnum= pd.Series(map(lambda x: x.isnumeric(),df["A"]))
print(df[notnum==False])
A
4 01t23
6 02t13
7 02t14
8 02t16
9 02t18
10 02t21
11 02t22
12 02t23
14 abcd
15 abcd1
16 tyu78890
17 hpsc77888
要么:
notnum= df["A"].apply(lambda x: x.isnumeric())
print(df[notnum==False])
A
4 01t23
6 02t13
7 02t14
8 02t16
9 02t18
10 02t21
11 02t22
12 02t23
14 abcd
15 abcd1
16 tyu78890
17 hpsc77888