question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Drop rows based on condition

See original GitHub issue

Here are 2 ways to drop rows from a pandas data-frame based on a condition:

  1. df = df[condition]

  2. 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:closed
  • Created 5 years ago
  • Reactions:8
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

6reactions
Ashishjshettycommented, May 4, 2018

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 needs

example:

condition= df.a==1
df.where(cond=condition,inplace=True)
4reactions
zhuhurencommented, Jun 29, 2018

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

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found