Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 正在尝试循环写入、覆盖_Python_Loops_Syntax_Pandas - Fatal编程技术网

Python 正在尝试循环写入、覆盖

Python 正在尝试循环写入、覆盖,python,loops,syntax,pandas,Python,Loops,Syntax,Pandas,当我开始这个项目的时候,我并不打算做像我现在尝试的那样多的迭代。如果我从帧列表中指定了一个特定帧,程序工作正常,将我需要的数据写入csv文件。但是,当我尝试遍历整个帧列表(总共6帧)时,输出列表只是被重写,当程序终止时,csv文件中只存在最后一个输出列表。我认为循环结构有意义,但我看不出错误 对不起,我知道我的代码很黑 # python_fitness_function import pandas as pd import itertools infile_path = "C:\\Users

当我开始这个项目的时候,我并不打算做像我现在尝试的那样多的迭代。如果我从帧列表中指定了一个特定帧,程序工作正常,将我需要的数据写入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))
#python_fitness_函数
作为pd进口熊猫
进口itertools
infle\u path=“C:\\Users\\Tim\\Dropbox\\Lela.com\u DrBx\\APR14\u query\u work\\”
df=pd.DataFrame.from_csv(填充路径+“mp_查看的项目_AGG_附属项目_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=组。获取组(“step two”)
df_ebeanstalk=groups.get_group(“ebeanstalk”)
df_lela=groups.get_group(“lela”)
df_electronics_express=组。获取组(“电子快车”)
数据帧=[df_cameta、df_compsource、df_ebeanstalk、df_electronics_express、df_lela、df_step_two]
#j=0

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

    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))
j=0

而j“它只是覆盖”-什么覆盖?问题出在哪里?具体是什么?@jornsharpe抱歉,edited基本上,根据我看到的缩进,循环不包含写入的块。循环和后面的块在同一个缩进中,这意味着循环首先完成,这意味着df_活动现在有第6项,然后writer代码执行