0

I want to delete the row that is column 'revenue' = 0 or 'Budget' = 0 from the movie table so I tried this code.

movies[(movies['revenue'] == 0) or (movies['Budget'] == 0)]

Then, I got a value error

So I tried this code as well

columns = ['budget', 'revenue']
df = movies.replace(0, pd.np.nan).dropna(axis=0, how='any', subset = columns).fillna(0).astype(int)

also I got an Future warning " The pandas.np module is deprecated and will be removed from pandas in a future version. Import numpy directly instead. plus, value error that traceback

momentum
  • 23
  • 5

3 Answers3

4

Try this, and use | bitwise operator (means or):

new = movies[(movies['revenue'] != 0) | (movies['budget'] != 0)]  

The code will create a new df with the rows that are not 0 in column or not 0 in budget.

Note that != means not equal. For extra information on operators, I find this link clear: https://www.w3schools.com/python/python_operators.asp

sophocles
  • 13,593
  • 3
  • 14
  • 33
1

You need to change the below line -

drop_idx = movies[(movies['revenue'] == 0) or (movies['Budget'] == 0)].index

to

drop_idx = movies[(movies['revenue'] == 0) | (movies['Budget'] == 0)].index

Once you have the drop indexes you can use drop to delete the indexes

movies = movies.drop(index=drop_idx)
Vaebhav
  • 4,672
  • 1
  • 13
  • 33
  • I tried but I got a KeyError: "['id' 'imdb_id' 'popularity' 'budget' 'revenue' 'original_title' 'cast'\n 'homepage' 'director' 'tagline' 'keywords' 'overview' 'runtime' 'genres'\n 'production_companies' 'release_date' 'vote_count' 'vote_average'\n 'release_year' 'budget_adj' 'revenue_adj' 'movies_count'] not found in axis" – momentum Dec 23 '20 at 10:08
  • Updated the answer to rectify this error – Vaebhav Dec 23 '20 at 11:30
0

You can't use the or operator there, you have to use the logical or | sign:

movies = movies[(movies['revenue'] == 0) & (movies['Budget'] == 0)]

Edit:

If you want to remove the rows with 0, try using the ~ sign:

movies = movies[~(movies['revenue'] == 0) & ~(movies['Budget'] == 0)]

I use & since OP said in the comments that he needs &

U13-Forward
  • 69,221
  • 14
  • 89
  • 114