1

enter image description here

(picture is a snapshot of my df)

I want to remove the rows that have a zero value. I don't want to modify the DataFrame, but create an assignment which reflects the DataFrame with no zero values in the 'amount spent' column.

2
  • Show us what you've tried so far Commented Nov 29, 2020 at 18:57
  • 1
    Example: df[df['amount_spent'] != 0] Commented Nov 29, 2020 at 18:58

1 Answer 1

2

This syntax can be used to view (index) a DataFrame based on a given condition:

# View whole DataFrame where `amount_spent` is not zero.
df[df['amount_spent'] != 0]

Here is the official pandas documentation on indexing.

What's happening? The df['amount_spent'] != 0 statement returns a 'mask' as a boolean array (Series), which is then passed into the 'rows parameter index' (if you will) of the overall DataFrame as: df[<masked_array>].

This will create a 'view' of the DataFrame and not alter it in any way.

Sign up to request clarification or add additional context in comments.

1 Comment

For anybody who finds this but wants to actually change the dataframe, you just assign it: df = df[df['amount_spent'] != 0]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.