我的函数查看 3 个数据帧,在不同日期之间进行过滤,并创建一个语句。
正如您所看到的,该函数一遍又一遍地重复使用相同的步骤,我想减少它们。
我相信使用 afor-loop
会有所帮助,但我不确定如何return
像现在这样在一小段中做出陈述
def stat_generator(df,date1,date2,df2,date3,date4,df4,date5,date6):
##First Date Filter for First Dataframe, and calculations for first dataframe
df['Announcement Date'] = pd.to_datetime(df['Announcement Date'])
mask = ((df['Announcement Date'] >= date1) & (df['Announcement Date'] <= date2))
df_new = df.loc[mask]
total = len(df_new)
better = df_new[(df_new['performance'] == 'better')]
better_perc = round(((len(better)/total)*100),2)
worse = df_new[(df_new['performance'] == 'worse')]
worse_perc = round(((len(worse)/total)*100),2)
statement1 = "During the time period between {} and {}, {} % of the students performed better. {} %
of the students performed worse" .format(date1,date2,better_perc,worse_perc)
##Second Date Filter for Second Dataframe, and calculations for second dataframe
df2['Announcement Date'] = pd.to_datetime(df2['Announcement Date'])
mask2 = ((df2['Announcement Date'] >= date3) & (df2['Announcement Date'] <= date4))
df_new2 = df2.loc[mask2]
total2 = len(df_new2)
better2 = df_new2[(df_new2['performance'] == 'better')]
better_perc2 = round(((len(better2)/total2)*100),2)
worse2 = df_new2[(df_new2['performance'] == 'worse')]
worse_perc2 = round(((len(worse2)/total2)*100),2)
statement2 = "During the time period between {} and {}, {} % of the students performed better. {} %
of the students performed worse" .format(date3,date4,better_perc2,worse_perc2)
##Third Date Filter for Third Dataframe, and calculations for third dataframe
www说
尚方宝剑之说
相关分类