简体   繁体   English

Python:如何根据 2 列中的条件过滤掉行

[英]Python: How to filter out rows based on a condition from 2 columns

I have a data frame DF in Python and I want to filter its rows based on 2 columns.我在 Python 中有一个数据框DF ,我想根据 2 列过滤它的行。

In particular, I want to remove the rows where orderdate is earlier than the startdate特别是,我想删除orderdate早于startdate的行

How can I reverse/opposite the condition inside the following code to achieve what I want?如何反转/反对以下代码中的条件以实现我想要的?

DF = DF.loc[DF['orderdate']<DF['startdate']]

I could reframe the code like below but it won't cover some rows that have NaT and I want to keep them我可以像下面那样重新构建代码,但它不会覆盖一些有 NaT 的行,我想保留它们

DF = DF.loc[DF['orderdate']>=DF['startdate']]

Inserting the ~ in front of the condition in parenthesis will reverse the condition and remove all the rows that do not satisfy it.在括号中的条件前面插入~将反转条件并删除所有不满足它的行。

DF = DF.loc[~(DF['orderdate']<DF['startdate'])]

1- loc takes the rows from the 'orderdate' column and compares them with the rows from the 'startdate' column. 1- loc从“orderdate”列中获取行,并将它们与“startdate”列中的行进行比较。 Where the condition is true, it returns the index of the lines and stores it in the ids array.如果条件为真,则返回行的索引并将其存储在 ids 数组中。

2 - The drop method deletes lines in the dataframe, the parameters are the array with the indices of the lines, and inplace = True, this ensures that the operation is performed on the dataframe itself, if it is False operation it will return a copy of the dataframe 2-drop方法删除dataframe中的行,参数为行索引的数组,inplace=True,这样保证操作是在dataframe本身上进行的,如果是False操作会返回一份dataframe

# Get names of indexes for which column orderdate > =  startdate
ids = DF.loc[DF['orderdate'] >= DF['startdate']].index
# Delete these row indexes from dataFrame
DF.drop(ids, inplace=True)

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

相关问题 pyspark中如何根据行列条件过滤多行 - How to filter multiple rows based on rows and columns condition in pyspark 如何根据包含条件过滤掉 pandas dataframe 行? - How to filter out pandas dataframe rows based on contains condition? 如何通过比较列从另一个数据帧中过滤掉一个python pandas数据帧的行? - How to filter out rows of one python pandas dataframe from another dataframe by comparing columns? 根据 python 的条件删除/过滤 JSON 中的行 - Remove/ filter rows in JSON based on condition with python Python:如何根据列名称中的子字符串匹配从数据集中过滤出列 - Python : How do you filter out columns from a dataset based on substring match in Column names 如何根据Numpy数组中的多列条件删除行? - How to delete rows based on multiple columns condition from Numpy array? 如何根据条件过滤出整个群体? - How to filter out an entire group based on condition? Pyspark - 根据一行中的条件过滤掉多行 - Pyspark - filter out multiple rows based on a condition in one row 如何根据条件在 python 中的列上拆分 74 行和 3234 列 dataframe - How to split a 74 rows and 3234 columns dataframe on columns in python based on condition 如何根据python中的条件从数据框中删除行? - How to drop rows from a dataframe based on condition in python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM