简体   繁体   English

Pandas DataFrame 不根据字符串值删除行

[英]Pandas DataFrame Not Dropping Rows based on String Value

I am having some trouble with filtering a dataset based on string values.我在根据字符串值过滤数据集时遇到了一些麻烦。 I have tried multiple methods and none of them seem to work.我尝试了多种方法,但似乎都不起作用。 I have data which looks like the following:我有如下所示的数据:

在此处输入图像描述

Some of the "CountryNames" in this dataset are "Unknown", like the following:此数据集中的某些“CountryNames”是“Unknown”,如下所示:

在此处输入图像描述

I would like to filter out the rows with the "Unknown" value in CountryNames.我想过滤掉 CountryNames 中具有“未知”值的行。 I have tried mulitple methods and none of them seem to work for some odd reason.我尝试了多种方法,但出于某种奇怪的原因,它们似乎都不起作用。 They just produce the exact same dataset as before.他们只是生成与以前完全相同的数据集。

Here's a snippet of my code:这是我的代码片段:

data = pd.read_excel(r"C:\Users\DylanNdengu\Downloads\combined_table.xlsx", index_col=False)

located_data = data[~data["CountryNames"].isin(["Unknown"])]

data and located data have the exact same shape, and the rows with Unknown are still there. data 和 located data 具有完全相同的形状,并且带有 Unknown 的行仍然存在。 Please also note I have also tried the following commands:另请注意,我还尝试了以下命令:

located_data = data[~data["CountryNames"].isin(["Unknown"])==True]
located_data = data[data["CountryNames"].isin(["Unknown"])==False]
located_data = data[data["CountryNames"]!="Unknown"]

All of these are not working either.所有这些都不起作用。 Please tell me what I am doing wrong and how to fix this.请告诉我我做错了什么以及如何解决这个问题。 The dtype for the CountryNames column is "object" if that helps.如果有帮助,CountryNames 列的 dtype 是“object”。

From your image examples it looks like the values in your data are actually misspelled as Uknown rather than the correct Unknown .从您的图像示例来看,您的数据中的值实际上被错误拼写为Uknown而不是正确的Unknown Otherwise your code seems correct.否则你的代码似乎是正确的。

Try this:尝试这个:

located_data = data[~data["CountryNames"].isin(["Uknown"])]

Alternatively you should fix your data by renaming the mispelled names with the correct ones, for example by using:或者,您应该通过使用正确的名称重命名拼写错误的名称来修复数据,例如使用:

data[data["CountryNames"]=="Uknown"] = "Unknown"

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM