简体   繁体   English

如何使用python删除-9999作为列值的数据帧的行?

[英]how to remove the rows of a dataframe with -9999 as column value using python?

for column in df:
print(column)
print((df[column] == -9999).value_counts()) 
print("")

modelMag_u
False    49977
True        23
Name: modelMag_u, dtype: int64

modelMag_i
False    49985
True        15
Name: modelMag_i, dtype: int64

Can anyone please help me how to remove the rows with -9999 as column value?任何人都可以帮助我如何删除以 -9999 作为列值的行吗?

Assuming the following: You want to delete a row if there is any column in that row that has value -9999假设以下内容:如果该行中有任何值为 -9999 的列,您想要删除该行

df[~(df == -9999).any(axis='columns')]

How does this work?这是如何运作的?

df == -9999 is run first. df == -9999首先运行。 This compares every cell to -9999, creating a dataframe of True and False values depending on content of each cell.这将每个单元格与 -9999 进行比较,根据每个单元格的内容创建 True 和 False 值的数据框。 We then use .any(axis='columns') on this dataframe.然后我们在这个数据.any(axis='columns')使用.any(axis='columns') any (with the axis=columns parameter) returns True for each row that has any true value. any (带有axis=columns 参数)对于具有any真值的每一行都返回真。 This is exactly those rows you want to remove.这正是您要删除的那些行。 The df[~ ... ] construct keeps any rows that is not True in the ... . df[~ ... ]构造保留df[~ ... ]中不为 True 的任何行。 What remains is any row that has no cell with value -9999.剩下的是没有值为 -9999 的单元格的任何行。

暂无
暂无

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

相关问题 使用列值的Python子集数据帧行 - Python Subset dataframe rows using a column value Python:在数据框中用相同的值填充特定列并删除无用的行 - Python : Fill a specific column with the same value in a Dataframe and remove the rows useless Python Pandas Dataframe按Timedelta列值删除行 - Python Pandas Dataframe Remove Rows by Timedelta Column Value 如果一列与值匹配,则从 dataframe 中删除行 - Python 3.6 - Remove rows from dataframe if one column matches a value - Python 3.6 如何根据列值 python 标记多个 dataframe 行 - How to flag multiple dataframe rows based on a column value python 如何使用Python删除Excel文件中具有特定值的列中的行 - How to remove rows in a column with certain value in Excel file with Python 给定唯一的列值,Pandas 数据框如何删除以行长小于数字为条件的行? - Pandas dataframe how to remove rows conditioned on the length of rows being smaller than a number, given a unique column value? 如何在 Python Dataframe 中使用基于 ZC1C425268E68385D1AB1A1FBZ 的条件的 select 行 - How to select rows in Python Dataframe with a condition based on a function using a column Python Pandas Dataframe,删除“无”是任何列中的值的所有行 - Python Pandas Dataframe, remove all rows where 'None' is the value in any column 如何使用 python 将 dataframe 中的行名称更改为列名称? - How to change rows name to column name in dataframe using python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM