Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas: Remove Row Based on Applying Function

Tags:

python

pandas

I have a Pandas DataFrame, df, and it has a column called _text. I want to remove all rows where applying sentence_count to the value in _text column is not 0.

How would I go about this?

Normally with Pandas, I do something like:

result_df = result_df[result_df['_text'] != '']

But now I'm not just using the value in result_df['_text], I'm filtering based on the value calculated by a function...

Thoughts?

Thanks!

like image 268
anon_swe Avatar asked Jul 25 '17 22:07

anon_swe


People also ask

How do I delete a row based on conditions in Pandas?

Use pandas. DataFrame. drop() method to delete/remove rows with condition(s).

How do I delete rows in Pandas based on multiple conditions?

Pandas provide data analysts a way to delete and filter data frame using dataframe. drop() method. We can use this method to drop such rows that do not satisfy the given conditions.


1 Answers

If the function takes the sentence as its sole argument,

result_df[result_df['_text'].apply(sentence_count) != 0]
like image 184
ayhan Avatar answered Sep 22 '22 05:09

ayhan