当我开始这个程序的时候,我不打算像我目前正在尝试的那样做更多的迭代。如果我从框架列表中指定了一个特定的框架,程序就可以正常工作,将我需要的数据写入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))我已经尝试了一段时间不同的空格更改,有点沮丧:
发布于 2014-04-29 18:57:59
代码似乎没有正确缩进。应该是这样的-
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))除此之外,我看不出它为什么要覆盖。
https://stackoverflow.com/questions/23372209
复制相似问题