【问题标题】:ValueError: Lengths must match to compare when match value from different dataframesValueError:当匹配来自不同数据帧的值时,长度必须匹配才能比较
【发布时间】:2020-08-02 16:33:02
【问题描述】:

我有一个offer_received_data这样的数据框:

            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    

还有一个像这样的数据框portfolio

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

我想从portfolio 获取customer_id 的信息,这些信息包含在offer_received_data 中。

这是我的代码:

# 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]

这会返回一个错误:

ValueError: Lengths must match to compare

有人可以看看,不知道如何修改这段代码,非常感谢。

【问题讨论】:

    标签: python pandas jupyter-notebook string-comparison


    【解决方案1】:

    您应该使用 isin ,并在末尾添加副本以避免将来的副本减少

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

    【讨论】:

    猜你喜欢
    • 2019-08-24
    • 1970-01-01
    • 2017-04-22
    • 2016-04-07
    • 2017-04-12
    • 2017-03-25
    • 2020-07-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多