从python中的Dataframe中删除数字行[关闭]

问题描述 投票:2回答:3

我有一个pandas.DataFrame如下,我只需删除那些只是数字的行。如何在python中仅使用正则表达式删除那些纯数字的行。

我需要删除那些只有数字,而不是那些有数字+字母的数字。

    df
    0
    0
    1
    1
    01t23
    2
    02t13
    02t14
    02t16
    02t18
    02t21
    02t22
    02t23
    40745
    abcd
    abcd1
    tyu78890
    hpsc77888
python regex pandas
3个回答
2
投票

对于名称为df的一列的数据框,您可以删除纯数字,如:

Code:

df['df'][~df['df'].str.isnumeric()]

Test Code:

   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()])

Results:

           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

2
投票

使用contains by regex ^\d+$ - 字符串开头,1位或更多位数字以及boolean indexing字符串结尾:

df = df[~df['df'].str.contains('^\d+$')]

to_numericisna

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

1
投票

你也可以使用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

要么:

一个map() lambda()组合:

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

要么:

Series.apply()

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
© www.soinside.com 2019 - 2024. All rights reserved.