我正在使用python并寻求有关如何简化以下代码的帮助。谢谢。
for i in range(1,9):
if i == 1:
wt_1 = gwt_2018[gwt_2018.WkNum == i]
elif i == 2:
wt_2 = gwt_2018[gwt_2018.WkNum == i]
elif i == 3:
wt_3 = gwt_2018[gwt_2018.WkNum == i]
elif i == 4:
wt_4 = gwt_2018[gwt_2018.WkNum == i]
elif i == 5:
wt_5 = gwt_2018[gwt_2018.WkNum == i]
elif i == 6:
wt_6 = gwt_2018[gwt_2018.WkNum == i]
elif i == 7:
wt_7 = gwt_2018[gwt_2018.WkNum == i]
else i == 8:
wt_8 = gwt_2018[gwt_2018.WkNum == i]
答案 0 :(得分:0)
您需要类似的东西吗?
l = [wt_1, wt_2, wt_3, wt_4, wt_5, wt_6, wt_7, wt_8]
for index, i in enumerate(l):
i = gwt_2018[gwt_2018.WkNum == index+1]
答案 1 :(得分:0)
是否必须将'wt_i'作为所有i的变量名?
我会改用字典:
wt = {}
for i in range(1,9):
wt[i] = gwt_2018[gwt_2018.WkNum == i]
答案 2 :(得分:0)
我找到了解决方案[链接](Create multiple dataframe using for loop in python 2.7)
gbl = globals()
for i in range(1,54):
gbl['df_'+str(i)] = gwt_2018[gwt_2018.WkNum == i]