Demo entry 6657329

Normalize Data

   

Submitted by anonymous on Nov 03, 2017 at 05:27
Language: Python 3. Code size: 2.0 kB.

import csv
data = []

def read_file():
    # read csv file into 2d array
    global data
    datafile = "Speed Dating Data.csv"
    data = list(csv.reader(open(datafile)))

def output_file():
    # output normalized csv file
    with open("output.csv","w",newline="") as f:
        writer = csv.writer(f)
        writer.writerows(data)

def nor_num(i,j):
    # normalize each partition of six columns in one row
    sum = 0; temp = j
    for k in range(0,6):    # calculate the sum for six attributes
        sum += float(data[i][temp])
        temp += 1
    if sum == 0:    # if attenees did not fill out this question, keep it blank
        return
    if sum != 100:  # if attenees did not finish in 1-100 scale, normalize it
        fra = 100 / sum
        for k in range(0,6):
            data[i][j] = round(float(data[i][j]) * float(100/sum),2)
            j += 1

def nor_col(i,start):
    # normalize each partition of six columns
    global data
    k = start
    while k <= start+6:
        if data[i][k] == '':
            data[i][k] = 0
        float(data[i][k])
        k += 1
    nor_num(i,start)

def nor_file():
    # Normalize all datas from attenees
    global data
    rownum = 1
    while rownum < len(data):
        nor_col(rownum,17);
        nor_col(rownum,69); nor_col(rownum,75); nor_col(rownum,81)
        nor_col(rownum,108)
        nor_col(rownum,122); nor_col(rownum,128);nor_col(rownum,134); nor_col(rownum,140)
        nor_col(rownum,161); nor_col(rownum,167); nor_col(rownum,173); nor_col(rownum,179)
        rownum += 1

'''
def imp_ph_o():
    for row in range(1,len(data)+1):
        if data[row][j] == 0 && data[row][j+1] == 0 && data[row][j+2] == 0 && data[row][j+3] == 0 && data[row][j+4] == 0 && data[row][j+5] == 0
            return
        average
'''

def main():
    global data
    read_file()
    nor_file()
    output_file()
    print("yeah")

main()

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).