Home > Software engineering >  How to get values in a column of a Dataframe that exist in another Dataframe with same column type
How to get values in a column of a Dataframe that exist in another Dataframe with same column type

Time:12-13

Given 2 Dataframe user_df and playlists_df with same column User_ID (both int64), how can I get a new dataframe consisting of values from user_df where the User_ID exists in playlist_df['User_ID']?

I tried:

users_to_survey = users_df[users_df['User_ID'] in playlists_df['User_ID']]

but got:

TypeError: 'Series' objects are mutable, thus they cannot be hashed

concat solution seems work but I just want to get specific values from users_df, not combining the two dataframes.

CodePudding user response:

You can't use in with Series. Instead, you can use isin on a Series to check if any of the values of that Series are in this one:

users_to_servey = users_df[playlists_df['User_ID'].isin(users_df['User_ID'])]
  • Related