如何从Pandas数据框中删除包含特定列中任何字符串的行 [英] How to Remove Rows from Pandas Data Frame that Contains any String in a Particular Column

查看:840
本文介绍了如何从Pandas数据框中删除包含特定列中任何字符串的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下格式的CSV数据:

I have CSV data in the following format:

+-------------+-------------+-------+
|  Location   | Num of Reps | Sales |
+-------------+-------------+-------+
| 75894       |           3 |    12 |
| Burkbank    |           2 |    19 |
| 75286       |           7 |    24 |
| Carson City |           4 |    13 |
| 27659       |           3 |    17 |
+-------------+-------------+-------+

Location列的数据类型为object.我想做的是删除所有具有非数字位置标签的行.因此,鉴于上表,我想要的输出将是:

The Location column is of the object datatype. What I would like to do is to remove all rows that have non-numeric Location labels. So my desired output, given the above table would be:

+----------+-------------+-------+
| Location | Num of Reps | Sales |
+----------+-------------+-------+
|    75894 |           3 |    12 |
|    75286 |           7 |    24 |
|    27659 |           3 |    17 |
+----------+-------------+-------+

现在,我可以通过以下方式对解决方案进行硬编码:

Now, I could hard code the solution in the following manner:

list1 = ['Carson City ', 'Burbank'];
df = df[~df['Location'].isin(['list1'])]

以下文章启发了我们

但是,我正在寻找的是一种通用解决方案,该解决方案适用于上面列出的任何类型的表.

However, what I am looking for is a general solution, that will work for any table of the type outlined above.

推荐答案

或者您可以

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



  Location  Num of Reps  Sales
0    75894            3     12
2    75286            7     24
4    27659            3     17

这篇关于如何从Pandas数据框中删除包含特定列中任何字符串的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆