Huzo
Huzo

Reputation: 1692

Creating a dataframe with a logic statement including other dataframes

I could not find a better title for my issue but the thing is that, let's assume we have a dataframe of film reviews. I want to create another dataframe that includes 1 star or 5 star reviews.

I came with this approach:

movie_class = movie[movie['stars'] == 1 | movie['stars'] == 5]

However, that gave me an error. But when I create the dataframe with only one condition, the code works fine.

movie = movie[movie['stars'] == 1]

Could not figure why.

Error:

    ---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-22-2fe827919d94> in <module>()
----> 1 movie_class = movie[movie['stars'] == 1 | movie['stars'] == 5]

~/anaconda3/lib/python3.6/site-packages/pandas/core/generic.py in __nonzero__(self)
   1119         raise ValueError("The truth value of a {0} is ambiguous. "
   1120                          "Use a.empty, a.bool(), a.item(), a.any() or a.all()."
-> 1121                          .format(self.__class__.__name__))
   1122 
   1123     __bool__ = __nonzero__

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

Upvotes: 0

Views: 35

Answers (1)

user7986928
user7986928

Reputation:

From what i understand from your question, this example may be what you want to achieve :

Generatung similar dataframe,

import pandas
import random

movie = [random.randint(0,5) for i in range(20)];
df = pandas.DataFrame(movie, columns = ['stars']);

To classify the 1 star and 5 star boolean independently :

one_star_bool = df['stars']==1;
five_star_bool = df['stars']==5;

Get the data :

one_star = df['stars'][one_star_bool];
five_star = df['stars'][five_star_bool];

Of course you can combine these two to get 1 star or 5 stars, but to get it directly you may use :

one_or_five = df['stars'][one_star_bool | five_star_bool];

Is this okay?

Upvotes: 1

Related Questions