csv 파일의 각 개별 열에서 스크립트를 실행하려고합니다. 파이썬에서 어떤 스크립트를 실행하고 싶은지를 파이썬에게 알려주는 방법을 알아 냈습니다. 그러나 필자는이 칼럼을 분석하고, 결과를 출력하고, 칼럼 2로 옮기고, 파일을 계속해서 실행하기를 원합니다. 내가 원하는 것은 "if etc goto etc"명령입니다. 간단한 oneliner로이 작업을 수행하는 방법을 찾았지만 더 큰 스크립트가 있습니다. 내가 뭔가를 놓친 것 같아서 도움이 될 것입니다. 마치 내가 내 데이터 (h = data)를 정의하는 곳으로 되돌아 갈 수 있지만 다음 열을 선택하라는 말처럼. 여기에 내 대본이있다.CSV의 다음 열로 돌아 가기
import numpy as np
import matplotlib.pyplot as plt
from pylab import *
import pylab
from scipy import linalg
import sys
import scipy.interpolate as interpolate
import scipy.optimize as optimize
a=raw_input("Data file name? ") #Name of the data file including the directory, must be .csv
datafile = open(a, 'r')
data = []
for row in datafile:
data.append(row.strip().split(',')) #opening and organizing the csv file
print('Data points= ', len(data))
print data
c=raw_input("Is there a header row? y/n?") #Remove header line if present
if c is ('y'):
del data[0]
data2=data
print('Raw data= ', data2)
else:
print('Raw data= ', data)
'''
#if I wanted to select a column
b=input("What column to analyze?") #Asks what column depth data is in
if b is 1:
h=[[rowa[i] for rowa in data] for i in range(1)] #first row
'''
h=data # all columns
g=reduce(lambda x,y: x+y,h) #prepares data for calculations
a=map(float, g)
a.sort()
print ('Organized data= ',a)
def GRLC(values):
'''
Calculate Gini index, Gini coefficient, Robin Hood index, and points of
Lorenz curve based on the instructions given in
www.peterrosenmai.com/lorenz-curve-graphing-tool-and-gini-coefficient-calculator
Lorenz curve values as given as lists of x & y points [[x1, x2], [y1, y2]]
@param values: List of values
@return: [Gini index, Gini coefficient, Robin Hood index, [Lorenz curve]]
'''
n = len(values)
assert(n > 0), 'Empty list of values'
sortedValues = sorted(values) #Sort smallest to largest
#Find cumulative totals
cumm = [0]
for i in range(n):
cumm.append(sum(sortedValues[0:(i + 1)]))
#Calculate Lorenz points
LorenzPoints = [[], []]
sumYs = 0 #Some of all y values
robinHoodIdx = -1 #Robin Hood index max(x_i, y_i)
for i in range(1, n + 2):
x = 100.0 * (i - 1)/n
y = 100.0 * (cumm[i - 1]/float(cumm[n]))
LorenzPoints[0].append(x)
LorenzPoints[1].append(y)
sumYs += y
maxX_Y = x - y
if maxX_Y > robinHoodIdx: robinHoodIdx = maxX_Y
giniIdx = 100 + (100 - 2 * sumYs)/n #Gini index
return [giniIdx, giniIdx/100, robinHoodIdx, LorenzPoints]
result = GRLC(a)
print 'Gini Index', result[0]
print 'Gini Coefficient', result[1]
print 'Robin Hood Index', result[2]
4-5 줄의 샘플 데이터를 게시하면 솔루션을 테스트하는 것이 더 쉬울 수도 있습니다. –
죄송합니다. 예를 들어 지니 (Gini) 계산기 일 뿐이므로 주간 급여 1 = 1234,2342,2234,2121,5677,4553, 급여 2 = 2342,23455,234,7564,43223,12213이라고 할 수 있습니다. csv 파일의 각 열에 급여가있는 경우. – user2843767