python - 为什么循环不迭代每一行?
问题描述
我有一个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
:
offer_id reward difficulty duration informational discount bogo mobile social web
0 ae264e3637204a6fb9bb56bc8210ddfd 10 10 7 0 0 1 1 1 0
这是我的代码:
# loop through each received offer
def get_offer_row(offer_received_data, portfolio):
offer_row = []
widgets=[
' [', progressbar.Timer(), '] ',
progressbar.Bar(),
' (', progressbar.ETA(), ') ',
]
for ind in progressbar.progressbar(range(len(offer_received_data)), widgets=widgets):
for i in range(offer_received_data.shape[0]):
# fetch an offer id
offer_id = offer_received_data.iloc[i]['offer_id']
print(offer_id)
# get offer_id row from portfolio
offer_row = portfolio.loc[portfolio['offer_id'] == offer_id]
return offer_row
get_offer_row(offer_received_data, portfolio)
这将返回:
9b98b8c7a33c4b65b9aebfe6a799e6d9
offer_id reward difficulty duration informational discount bogo mobile social web
3 9b98b8c7a33c4b65b9aebfe6a799e6d9 5 5 7 0 0 1 1 0 1
它只返回一行,根本不迭代每一行,有人可以看看我的代码,我做错了什么?非常感谢。
解决方案
你return
在迭代循环中。将其移出循环或将其更改为yield
推荐阅读
- c - What does this thing: "\x30\xff\xff\x31\xff\xff\xff\xff" called?
- unit-testing - 来自分支的 Jenkins 测试配置
- apache-spark - 何在 Apache spark LinearRegression 模型中设置正则化
- reactjs - 在 JWT 到期时反应 Redux 用户注销
- javascript - 如何在 JSDoc 中记录有关对象属性的更复杂信息?
- php - 如何在文字中回显变量
- xamarin - 如何在 DatePickerDialog 中以越南语显示月份和日期?
- java - WildFly 部署错误 - DeploymentUnitProcessingException
- java - 在不同拓扑上运行 K 最短路径算法并添加地图链接
- uml - 你如何在序列图中表示 this.println?