Python 3.x 减少熊猫中for循环的执行时间
我被困在熊猫执行循环 下面是代码片段Python 3.x 减少熊猫中for循环的执行时间,python-3.x,pandas,dataframe,Python 3.x,Pandas,Dataframe,我被困在熊猫执行循环 下面是代码片段 for ix, pt in result.iterrows(): for index,row in frame_SuggestedDose.iterrows(): isTrue = False if (pt[0]==row[0] and pt[4]==row[10]): # print("found") pt[8] = row[2] isTrue
for ix, pt in result.iterrows():
for index,row in frame_SuggestedDose.iterrows():
isTrue = False
if (pt[0]==row[0] and pt[4]==row[10]):
# print("found")
pt[8] = row[2]
isTrue = True
if(isTrue or pt[4]>datetime.now().date()):
break
result.loc[ix] = pt
在上面的代码中,0,10,2,4是数据帧中列的索引
如果结果患者id和帧建议的患者id相同,并且日期相同,我想将值从帧建议的状态复制到结果
结果帧的头部:
patientId Date IntervalDate IntervalName start_dt Dose FastingBloodGlucose IntervalSuggestedReason IntervalStatus BGL SuggestedDose
006b5d 2017-09-08 20:30:00 2017-09-08 20:30:00 Int1 2017-09-08 NaN NaN suggested_dose_reason_new_care_plan NaN NaN 14.0
对于框架_建议的框架
patientId category value units effective status fasting hypo suggestedDose suggestedReason effective_dt effective_tm dailyDoseTime dose_dt dose_tm
006b5d51 DOSE_SUGGESTION 14.0 units 2017-09-08 20:30:00 active 0.0 0.0 0.0 suggested_dose_reason_new_care_plan 2017-09-08 20:30:00 1970-01-01 20:30:00 1970-01-01 20:30:00
执行大约需要2个小时
如何减少执行时间
我正在使用Jupyter笔记本请尝试以下代码
result = result.drop("value",axis=1)
result = pd.merge(result,
frame_SuggestedDose[["patientId","effective_dt","value"]],
left_on=["patientId","Date"],
right_on=["patientId","effective_dt"],
how="left")
在熊猫中,您几乎不必使用循环。请把你的df的标题和问题贴出来,你可能会得到更好的答案solution@Vaishali请编辑帖子!您能否更具体地描述一下您试图用代码片段实现的目标?正如@Vaishali所建议的,用一个示例数据框发布一个玩具问题,清楚地解释您试图实现的目标。如果没有问题的背景,几乎不可能建议如何改进代码。@doktakay post edited。