简体   繁体   English

删除pandas数据框中具有混合数据类型的所有行,这些数据类型包含多列中的特定字符串

[英]remove all rows in pandas dataframe with mixed data types that contain a specific string in multiple columns

How can I remove all rows in a dataframe if a row contains '9999-Don't Know' in any column? 如果某行的任何列中包含“ 9999-未知”,如何删除数据框中的所有行?

I have been able to find solutions that delete rows based on format of value (string, numerical, etc.) in the entire dataframe, or delete rows based on values in a specific column, or delete rows from a dataframe that has few columns by using their names. 我已经能够找到解决方案,这些解决方案可以基于整个数据框中的值格式(字符串,数字等)删除行,或者基于特定列中的值删除行,或者从具有几列的数据框中删除行使用他们的名字。

This is the closest thing I found but this solution doesn't work for me because I cannot enter all the column names due to sheer volume (76+ columns). 是我找到的最接近的东西,但该解决方案对我而言不起作用,因为由于数量庞大(超过76列),我无法输入所有列名。

Below is a sample dataset 以下是样本数据集

pd.DataFrame.from_items([('RespondentId', ['1ghi3g','335hduu','4vlsiu4','5nnvkkt','634deds','7kjng']), ('Satisfaction - Timing', ['9-Excellent','9-Excellent','9999-Don\'t Know','8-Very Good','1-Very Unsatisfied','9999-Don\'t Know']),('Response Speed - Time',['9999-Don\'t Know','9999-Don\'t Know','9-Excellent','9-Excellent','9-Excellent','9-Excellent'])])

After removing the 4 rows that contain '9999-Don't Know', the output should look like this so I can write a new Excel file with the cleaned up data. 删除包含“ 9999-未知”的4行之后,输出应如下所示,这样我就可以使用清理后的数据编写一个新的Excel文件。

pd.DataFrame.from_items([('RespondentId', ['5nnvkkt','634deds']), ('Satisfaction - Timing', ['8-Very Good','1-Very Unsatisfied']),('Response Speed - Time',['9-Excellent','9-Excellent'])]) 

Use 采用

In [677]: df[~(df == "9999-Don't Know").any(axis=1)]
Out[677]:
  RespondentId Satisfaction - Timing Response Speed - Time
3      5nnvkkt           8-Very Good           9-Excellent
4      634deds    1-Very Unsatisfied           9-Excellent

Or 要么

In [683]: df[(df != "9999-Don't Know").all(axis=1)]
Out[683]:
  RespondentId Satisfaction - Timing Response Speed - Time
3      5nnvkkt           8-Very Good           9-Excellent
4      634deds    1-Very Unsatisfied           9-Excellent

Same as 如同

In [686]: df[~df.eq("9999-Don't Know").any(axis=1)]
Out[686]:
  RespondentId Satisfaction - Timing Response Speed - Time
3      5nnvkkt           8-Very Good           9-Excellent
4      634deds    1-Very Unsatisfied           9-Excellent

Or 要么

In [687]: df[df.ne("9999-Don't Know").all(axis=1)]
Out[687]:
  RespondentId Satisfaction - Timing Response Speed - Time
3      5nnvkkt           8-Very Good           9-Excellent
4      634deds    1-Very Unsatisfied           9-Excellent

With mixed column types, see @PiR's comment df.astype(object) 对于混合列类型,请参见@PiR的注释df.astype(object)

In [695]: df[df.astype(object).ne("9999-Don't Know").all(axis=1)]
Out[695]:
  RespondentId Satisfaction - Timing Response Speed - Time
3      5nnvkkt           8-Very Good           9-Excellent
4      634deds    1-Very Unsatisfied           9-Excellent

暂无
暂无

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

相关问题 如何在混合类型的列的Pandas Dataframe中删除所有数字列都为零的行? - How to remove rows where all numerical columns contain zero in Pandas Dataframe with mixed type of columns? python:删除包含字符串的pandas数据帧中的所有行 - python: remove all rows in pandas dataframe that contain a string Python 删除特定字符串之后的所有内容并循环遍历数据帧中多列中的所有行 - Python remove everything after specific string and loop through all rows in multiple columns in a dataframe 删除 pandas 中多列中包含一段字符串的所有行 - Remove all rows containing a piece of a string in multiple columns in pandas 在 Pandas 中为特定列绘制多行数据框 - Plot multiple rows of dataframe in pandas for specific columns 如果所有列都包含特定模式,请在pandas数据框中选择行 - Select rows in pandas dataframe if all the columns contain certain pattern 如果任何特定列包含特定值,则删除 pandas 数据框中的行 - Remove rows in pandas dataframe if any of specific columns contains a specific value 如果列表包含特定值,如何删除所有 Pandas 行? - How to remove all Pandas rows of lists if they contain specific values? 删除包含 Pandas 中 dataframe 中的任何字符串的所有行 - Drop all rows that contain any string from a dataframe in Pandas 在pandas数据框中选择仅包含给定字符串的行(具有多个字符串) - Select rows (with multiple strings) in pandas dataframe that contain only a given string
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM