首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >试图循环写,覆盖

问试图循环写,覆盖
EN

Stack Overflow用户
提问于 2014-04-29 18:12:45
回答 1查看 103关注 0票数 0

当我开始这个程序的时候,我不打算像我目前正在尝试的那样做更多的迭代。如果我从框架列表中指定了一个特定的框架,程序就可以正常工作,将我需要的数据写入csv文件。但是,当我试图遍历整个帧列表(总共6帧)时,输出列表只是被重写,当程序终止时,csv文件中只存在最后一个输出列表。我认为循环结构是有意义的,但我看不到错误

我知道我的代码很麻烦,很抱歉。

代码语言:javascript
复制
# 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))

我已经尝试了一段时间不同的空格更改,有点沮丧:

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-04-29 18:57:59

代码似乎没有正确缩进。应该是这样的-

代码语言:javascript
复制
    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))

除此之外,我看不出它为什么要覆盖。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23372209

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档