Drop rows based on condition
See original GitHub issueHere are 2 ways to drop rows from a pandas data-frame based on a condition:
-
df = df[condition] -
df.drop(df[condition].index, axis=0, inplace=True)
The first one does not do it inplace, right?
The second one does not work as expected when the index is not unique, so the user would need to reset_index() then set_index() back.
Question
Would it be possible to have column dropping based directly on the condition?
e.g.
df.drop(condition, axis=0, inplace=True)
Issue Analytics
- State:
- Created 5 years ago
- Reactions:8
- Comments:10 (5 by maintainers)
Top Results From Across the Web
Drop rows from the dataframe based on certain condition ...
Drop rows from the dataframe based on certain condition applied on a column ; import pandas as pd · # Print the shape...
Read more >How to delete rows from a pandas DataFrame based on a ...
(Note: I know I can use df.dropna() to get rid of rows that contain any NaN , but I didn't see how to...
Read more >Pandas Drop Rows With Condition - Spark by {Examples}
drop() method takes several params that help you to delete rows from DataFrame by checking condition. When condition expression satisfies it returns True...
Read more >How to Drop Rows in Pandas DataFrame Based on Condition
Method 1: Drop Rows Based on One Condition ... Any row that had a value less than or equal to 8 in the...
Read more >Delete Rows Based on a Cell Value (or Condition) in Excel ...
Delete Rows Based on a Numeric Condition · Select any cell in the data · Click on the Data tab · In the...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

you could probably look at DataFrame.where
https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.where.html#pandas.DataFrame.where, it has inplace=True flag so it would work for you needsexample:
Here’s my solution. (I can’t get the format of the code to work properly here.)
def drop_rows_by_condition(data, cond, temp_col=‘temp_col’): “”“When there are duplicates in index, dropping rows by condition is not straight forward. If 2 rows have the same index, one meets the condition and the other doesn’t, the 2 rows tend to be both dropped. This function solves this problem by using a temporary column. return: dropped rows “”” data_meet_criteria = data[cond] data[temp_col] = cond data.loc[cond, temp_col] = np.nan data.dropna(subset=[temp_col], inplace=True, axis=0) data.drop(temp_col, axis=1, inplace=True) return data_meet_criteria