Search code examples
pythonpandasjupyter-notebookstring-comparison

ValueError: Lengths must match to compare when match value from different dataframes


I have a dataframe offer_received_datalike this:

            customer_id                 offer_id                              time  offer_received offer_viewed 
0   78afa995795e4d85b5d9ceeca43f5fef    9b98b8c7a33c4b65b9aebfe6a799e6d9       0.0    1            0    
53176   78afa995795e4d85b5d9ceeca43f5fef    5a8bc65990b245e5a138643cd4eb9837   7.0    1            0    
150598  78afa995795e4d85b5d9ceeca43f5fef    ae264e3637204a6fb9bb56bc8210ddfd   17.0   1            0    

and a dataframe portfolio like this:

     customer_id                     reward difficulty  duration    informational   discount    bogo    mobile  social  web
0   ae264e3637204a6fb9bb56bc8210ddfd    10  10  7   0   0   1   1   1   0

I wanted to get the information from portfolio for customer_id which are included in the offer_received_data.

Here is my code:

# make a list of the unique customer_ids from offer_df
customer_ids = offers_df['customer_id'].unique()

# match customer_ids between profile dataframe and the list above
customer = profile[profile['customer_id'] == customer_ids]

This returns an error:

ValueError: Lengths must match to compare

Can someone have a look, not sure how to modify this code, many thanks.


Solution

  • You should use isin , Also add copy at the end to avoid future copy waning

    customer = profile[profile['customer_id'].isin(customer_ids)].copy()