简体   繁体   English

如何从包含特定列中的任何字符串的Pandas数据框中删除行

[英]How to Remove Rows from Pandas Data Frame that Contains any String in a Particular Column

I have CSV data in the following format: 我有以下格式的CSV数据:

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

The Location column is of the object datatype. Location列是object数据类型。 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'])]

Which was inspired by the following post: 其灵感来自以下帖子:

How to drop rows from pandas data frame that contains a particular string in a particular column? 如何从包含特定列中特定字符串的pandas数据框中删除行?

However, what I am looking for is a general solution, that will work for any table of the type outlined above. 但是,我正在寻找的是一般解决方案,适用于上述类型的任何表。

Or you could do 或者你可以做到

df[df['Location'].str.isnumeric()]
Location  Num of Reps  Sales
0    75894            3     12
2    75286            7     24
4    27659            3     17

You can use pd.to_numeric to coerce non numeric values to nan and then filter based on if the Location is nan : 您可以使用pd.to_numeric将非数字值强制nan ,然后根据Location是否为nan进行过滤:

df[pd.to_numeric(df.Location, errors='coerce').notnull()]

#Location  Num of Reps  Sales
#0  75894            3     12
#2  75286            7     24
#4  27659            3     17
In [139]: df[~df.Location.str.contains('\D')]
Out[139]:
  Location  Num of Reps  Sales
0    75894            3     12
2    75286            7     24
4    27659            3     17
df[df['Location'].str.isdigit()]


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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从包含特定列中的特定字符串(多个)的 Pandas 数据框中删除行? - How to drop rows from pandas data frame that contains a particular string(multiple) in a particular column? 在python中,如何从任何列中存在特定字符串的数据框中获取行(字符串值) - In python,how to get the rows from a data frame where a particular string is present in any of the column (String value) 如果列的字符串值包含特定模式,如何从 pandas 数据帧中提取整行 - How to extract entire rows from pandas data frame, if a column's string value contains a specific pattern 如何找到分组的数据框的两列行的交集并从包含它的单元格中删除该值? - How to find intersection of two column rows of data frame that is grouped by and remove that value from cells that contains it? 如何从 pandas 列中删除特定字符串匹配的单元格 - How to Remove the cells from pandas column where particular string matched 如果 pandas 数据帧的计数很小,如何删除某些行 - How to remove certain rows from pandas data frame if the counts of it is small 有条件地从熊猫数据框中删除行 - remove rows from pandas data frame with condition 熊猫数据框找到具有特定列值的所有行? - pandas data frame find all rows with particular column value? Pandas:删除任何列包含某个子字符串的所有行 - Pandas: Remove all rows where any of the column contains a certain substring 如何从 pandas 数据框的列值创建新行 - How to create a new rows from column values of pandas data frame
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM