ValueError: Lengths must match to compare when match value from different dataframes
ValueError: Lengths must match to compare when match value from different dataframes
我有一个这样的数据框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]
这returns一个错误:
ValueError: Lengths must match to compare
谁能看看,不知道如何修改这段代码,非常感谢。
你应该使用 isin
,并在末尾添加副本以避免将来的副本减弱
customer = profile[profile['customer_id'].isin(customer_ids)].copy()
我有一个这样的数据框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]
这returns一个错误:
ValueError: Lengths must match to compare
谁能看看,不知道如何修改这段代码,非常感谢。
你应该使用 isin
,并在末尾添加副本以避免将来的副本减弱
customer = profile[profile['customer_id'].isin(customer_ids)].copy()