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

remove row if specific columns does not contain digit in pandas

我有以下 pandas 数据框:

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

我想删除 'WKT' 列中不包含任何数字的行,例如第 1 行。 我看到有函数 isnumeric() 但我不想检查单元格中的所有字符是否都是数字,但只有当它包含数字或 nit 时,如果不删除它。

我想要的输出应该是这样的:

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

您可以在 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)

您可以使用 .str.contains 和过滤器,这里 \d+ 将匹配多个数字:

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

或者,您可以通过

删除包含“None”的数据点
df[~df["WKT"].str.contains("None")]


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