Question

When I started this program, I didn't intend to do as much iteration as I currently am trying. If I indicate a specific frame from the frames list, the program works fine, writing the data I need to a csv file. However, when I try to iterate through the entire list of frames (6 in total), the output lists are simply over-written, with only the last output list existing in the csv file when the program terminates. I think the loop structure make sense but I can't see the error

I know my code is pretty hacky, I'm sorry.

# 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))

I've tried various whitespace changes for a while now, getting a bit frustrated :(

Was it helpful?

Solution

The code does not seem properly indented. It should look like this -

    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))

Other than that I don't see a reason why it overwrites.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top