当我开始这个程序时,我并没有打算像我目前所做的那样进行尽可能多的迭代。如果我从帧列表中指出一个特定的帧,程序工作正常,将我需要的数据写入csv文件。但是,当我尝试遍历整个帧列表(总共6个)时,输出列表被简单地重写,当程序终止时,只有最后一个输出列表存在于csv文件中。我认为循环结构有意义但我无法看到错误
我知道我的代码非常黑,我很抱歉。
# python_fitness_function
import pandas as pd
import itertools
infile_path = "C:\\Users\\Tim\\Dropbox\\Lela.com_DrBx\\APR14_query_work\\"
df = pd.DataFrame.from_csv(infile_path + "mp_viewed_item_AGG_affiliate_item_TOP_10.csv", sep=',', index_col=True)
groups = df.groupby('Affiliate_ID')
df_cameta = groups.get_group("cameta")
df_compsource = groups.get_group("compsource")
df_step_two = groups.get_group("step-two")
df_ebeanstalk = groups.get_group("ebeanstalk")
df_lela = groups.get_group("lela")
df_electronics_express = groups.get_group("electronic-express")
dataframes = [df_cameta, df_compsource, df_ebeanstalk, df_electronics_express, df_lela, df_step_two]
# j = 0
# while j <= 5:
#
#
#
df_active = dataframes[0] # dataframes[j]
# j += 1
#
#
#
cols = list(df_active.columns)
print df_active['Affiliate_ID'][1]
while 'Affiliate_ID' in cols:
cols.remove('Affiliate_ID')
while 'Item_ID' in cols:
cols.remove('Item_ID')
while 'N_BREAK' in cols:
cols.remove('N_BREAK')
# print cols
Motivator_list = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L"]
index = 0
ABSDEV_SUM = 0
output = open('absdev.csv', 'w')
output.write(str(df_active['Affiliate_ID'][1]) + ',' + '\n')
for i in cols:
column = df_active[i]
AbsDev = sum(abs(pair[1] - pair[0]) for pair in itertools.combinations(column, 2))
ABSDEV_SUM = ABSDEV_SUM + AbsDev
output.write("Motivator_" + str(Motivator_list[index]) + "_Mean_AbsDev ," + str(AbsDev) + "\n")
print "Motivator_" + Motivator_list[index] + "_Mean_AbsDev"
print AbsDev
index += 1
print "Total Sum of pairwise deviations "
print ABSDEV_SUM
output.write("Total Sum of pairwise deviations ," + str(ABSDEV_SUM))
我已经尝试了一段时间的各种空白变化,让人有点沮丧:(
答案 0 :(得分:1)
代码似乎没有正确缩进。它看起来应该是这样的 -
j = 0
while j <= 5:
df_active = dataframes[j]
j += 1
cols = list(df_active.columns)
......................................
......................................
output.write("Total Sum of pairwise deviations ," + str(ABSDEV_SUM))
除此之外,我没有看到它被覆盖的原因。