我正在尝试使用python的CSV模块来修改CSV文件。该文件代表一个股票并列出(作为列)日期,开盘价,高价,低价,收盘价和当天的交易量。我想要做的是通过对现有数据执行代数来创建多个新列。例如,我想创建一个列,从开放价格到任何特定日期的高价格,以及从昨天接近今天收盘时的百分比变化(这里没有结束,现在正在考虑要添加的10列)。
有一种紧凑的方法吗?截至目前,我正在打开原始文件并将列表读入感兴趣的值。然后使用该列表在一些临时文件上写入修改后的值。然后使用for循环写入一些新文件并添加每个电子表格中的行。然后将该新文件的全部内容写入原始csv,因为我希望保留csv(ticker.csv)的名称。
希望我已经明确了我的问题。如果您想要任何澄清或进一步的细节,请不要犹豫。
编辑:我已经为下面的一个函数添加了一段代码。该函数旨在创建一个新列,其变化百分比从昨天接近今天收盘。
def add_col_pchange(ticker):
"""
Add column with percent change in closing price.
"""
original = open('file1', 'rb')
reader = csv.reader(original)
reader.next()
close = list()
for row in reader:
# build list of close values; entries from left to right are reverse chronological
# index 4 corresponds to "Close" column
close.append(float(row[4])
original.close()
new = open(file2, 'wb')
writer = csv.writer(new)
writer.writerow(["Percent Change"])
pchange = list()
for i in (0, len(close)-1):
x = (close[i]-close[i+1])/close[i+1]
pchange.append(x)
new.close()
# open original and new csv's as read, write out to some new file.
# later, copy that entire file to original csv in order to maintain
# original csv's name and include new data
答案 0 :(得分:0)
希望这有帮助
def add_col_pchange(ticker):
"""
Add column with percent change in closing price.
"""
# always use with to transparently manage opening/closing files
with open('ticker.csv', 'rb') as original:
spam = csv.reader(original)
headers = spam.next() # get header row
# get all of the data at one time, then transpose it using zip
data = zip(*[row for row in spam])
# build list of close values; entries from left to right are reverse chronological
# index 4 corresponds to "Close" column
close = data[4] # the 5th column has close values
# use map to process whole column at one time
f_pchange = lambda close0, close1: 100 * (float(close0) - float(close1)) / float(close1)
Ndays = len(close) # length of table
pchange = map(f_pchange, close[:-1], close[1:]) # list of percent changes
pchange = (None,) + tuple(pchange) # add something for the first or last day
headers.append("Percent Change") # add column name to headers
data.append(pchange)
data = zip(*data) # transpose back to rows
with open('ticker.csv', 'wb') as new:
spam = csv.writer(new)
spam.writerow(headers) # write headers
for row in data:
spam.writerow(row)
# open original and new csv's as read, write out to some new file.
# later, copy that entire file to original csv in order to maintain
# original csv's name and include new data
你应该看看numpy;您可以使用loadtxt()
和矢量数学,但@lightalchemist是正确的,pandas就是为此设计的。