lundi 30 mai 2016

python pandas dataframe index match

In a python pandas dataframe "df", I have the following three columns:

song_id | user_id | play_count

I have a rating table I invented based on play_count (how many times a user listened to a song):

play_count | rating
1-33       | 1
34-66      | 2
67-99      | 3   
100-199    | 4
>200       | 5

I am trying to add a column "rating" to this table based on play count. For example, if play_count=2, the rating will be "1".

So it looks like this

song_id | user_id | play_count | rating
X232    | u8347   | 2          | 1
X987    | u3701   | 50         | 2
X271    | u9327   | 10         | 1
X523    | u1398   | 175        | 4

In excel I would do this with match/index, but I don't know how to do it in python/pandas.

Would it be a combination of an if/else loop and isin?

Aucun commentaire:

Enregistrer un commentaire