简体   繁体   中英

Map list values to dataframe columns to count number of positive appearance [python]

I have a list of predictions like this (where every number is a movie id):

50 
2 
573
.
.
117 
173 

I have a pivoted pandas dataframe:

movie_id  1     2     3     4     5     ...  1678  
user_id                                 ...                              
1683       1.0   1.0   Nan   0.0   0.0  ...   NaN  
1684       1.0   NaN   NaN   NaN   NaN  ...   0.0  
1685       NaN   NaN   1.0   NaN   NaN  ...   NaN   
1686       1.0   NaN   NaN   0.0   NaN  ...   1.0
1687       1.0   0.0   NaN

What i want is for a specific user_id, to get the count of: the movie_ids in the predictions list that also have a value of 1.0 in my dataframe.

For example, my list has the movie_id = 2. If i'm examinign user 1683 then the count=count +1, cause movie 2 has a value of 1.0 in my dataframe

If you want to count the occurrences of the movie_ids for any user_id in your datagram, this may work:

movie_ids = [50, 2, 573, 117, 173] # your movie ids
user_id = 1683

user_movies = df.loc[user_id, movie_ids] # get particular movies for the given user

summed = np.nansum(user_movies) # sums all number in user_movies, neglects nans

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM