如果特定的列在熊猫中不包含数字,则删除行

时间:2020-10-29 09:27:06

标签: python pandas string digits

我有以下熊猫数据框:

[{'column': 'NRX_TOTAL', 'severity': 3, 'threshold': 0.25}, {'column': 'TRX_TOTAL', 'severity': 3, 'threshold': 0.25}]

我想删除“ WKT”列中不包含任何数字的行,例如第1行。 我看到它们是作为isnumeric()的函数,但是我不想检查单元格中的所有字符是否都是数字,而是仅检查它是否包含数字或尼特,是否不删除它。

我想要的输出应如下所示:

>>>ID      WKT
0 4272   Point(4.21189  3.1298)
1 2345   Point(None None)
2 1254   Point (3.8945 4.6712)
...

3 个答案:

答案 0 :(得分:3)

IIUC,您可以在WKT列上使用str.contains方法调用

df[df['WKT'].str.contains('\d')]

     ID                     WKT
0  4272  Point(4.21189  3.1298)
2  1254   Point (3.8945 4.6712)

\d

\d matches a digit (equal to [0-9])

+ Quantifier — Matches between one and unlimited times, 
as many times as possible, giving back as needed (greedy)

答案 1 :(得分:2)

您可以将.str.contains与过滤器配合使用,此处\d+将匹配多个数字:

df = df[df['WKT'].str.contains(r'\d+')]

答案 2 :(得分:1)

或者,您可以删除包含“无”的数据点,

df[~df["WKT"].str.contains("None")]


    ID      WKT
0   4272    Point(4.21189 3.1298)
2   1254    Point(3.8945 4.6712)
相关问题