Malintha
Malintha

Reputation: 4776

Pandas : Getting indexes of a series providing multiple filters

I am iterating over a dataframe. In each iteration, I need to get indexes of the series where the value is equal 5 or 4. Following code gives me the indexes where the value is 5 ( another problem: actually i am getting both index and the value. Another problem is getting only the index. I tried print row[row ==5].index[0] but it does not do the job for me)

def get_top_rated_movies(user_ratings):
for index, row in user_ratings.iterrows():
    print row[row == 5]

I think I have to do row[row == 5 || row == 4] kind of a thing but have no idea.

here 'user_ratings' is a dataframe. Each row is a user and column is a movie where I store users ratings for movies. I need to get the movies which got 4 or 5 ratings for each user. So, I iterate over the dataframe and above code gives me (both movie id and rating)

movie_id
1      5
9      5
13     5
15     5
16     5
19     5
32     5
42     5
45     5
48     5
50     5
55     5
57     5
59     5
87     5

Upvotes: 1

Views: 1441

Answers (1)

jezrael
jezrael

Reputation: 863791

I think you can use boolean indexing:

If user_ratings is Series:

user_ratings[(user_ratings == 5) | (user_ratings == 4)]

Or better:

user_ratings[user_ratings.isin([4,5])]

And if need indexes filter:

user_ratings.index[(user_ratings == 5) | (user_ratings == 4)]
user_ratings.index[user_ratings.isin([4,5])]

Or first filter Series and then get indexes:

user_ratings[(user_ratings == 5) | (user_ratings == 4)].index
user_ratings[user_ratings.isin([4,5])].index

Upvotes: 2

Related Questions