从CSV文件中计算元组列表的平均值
在一个个人项目中,我有一个相当大的 .CSV 文件,里面是苹果公司过去的股票数据。我已经写了一个函数,使用 csv 模块来读取这些数据,并打印出日期和每个月的收盘价:
这里是元组格式的一个例子:
('2012-03-24' , '122.10')
现在我想要计算每个月的数据平均值,并重新生成元组列表。
有没有人有什么建议?我还是个初学 Python 的学生。
def get_list_data(file_obj, column_number):
with open("table.csv", "r") as f:
reader = csv.reader(f, delimiter=',')
for row in reader:
data = row[0] , row[column_number] #Data and column data
list_of_tuples = tuple(data)
print(list_of_tuples)
return list_of_tuples
def average_data(list_of_tuples): #This is where I am stuck
2 个回答
1
如果你正在自学Python,可以尝试自己用csv
库写一个读取器,然后自己计算平均值。这是个很好的练习。
不过,如果你想少写点代码,把更多时间花在分析上,可以使用像pandas
这样的工具(或者至少用numpy
)。pandas
库在这类数据分析方面非常出色。
下面的ipython会话展示了使用pandas
进行这些计算是多么简单。(如果你还没用过ipython,我强烈建议你去学习一下。)在这个会话中,我读取了一个包含苹果股票数据的CSV文件。数据文件'aapl.csv'的内容如下:
Date,Open,High,Low,Close,Volume,Adj Close
2013-02-25,453.85,455.12,442.57,442.80,13276100,442.80
2013-02-22,449.25,451.60,446.60,450.81,11798600,450.81
2013-02-21,446.00,449.17,442.82,446.06,15970800,446.06
2013-02-20,457.69,457.69,448.80,448.85,17010800,448.85
2013-02-19,461.10,462.73,453.85,459.99,15563700,459.99
2013-02-15,468.85,470.16,459.92,460.16,13990900,460.16
2013-02-14,464.52,471.64,464.02,466.59,12688400,466.59
...
1984-09-14,27.62,28.50,27.62,27.87,8826400,3.13
1984-09-13,27.50,27.62,27.50,27.50,7429600,3.09
1984-09-12,26.87,27.00,26.12,26.12,4773600,2.94
1984-09-11,26.62,27.37,26.62,26.87,5444000,3.02
1984-09-10,26.50,26.62,25.87,26.37,2346400,2.97
1984-09-07,26.50,26.87,26.25,26.50,2981600,2.98
导入pandas库:
In [1]: import pandas as pd
将数据读入一个DataFrame,并使用'Date'列作为索引:
In [2]: aapl = pd.read_csv('aapl.csv', index_col=0, parse_dates=True)
将索引按升序排序:
In [3]: aapl = aapl.sort()
查看前几条记录:
In [4]: aapl.head()
Out[4]:
Open High Low Close Volume Adj Close
Date
1984-09-07 26.50 26.87 26.25 26.50 2981600 2.98
1984-09-10 26.50 26.62 25.87 26.37 2346400 2.97
1984-09-11 26.62 27.37 26.62 26.87 5444000 3.02
1984-09-12 26.87 27.00 26.12 26.12 4773600 2.94
1984-09-13 27.50 27.62 27.50 27.50 7429600 3.09
将数据重新采样为按月统计。默认情况下,会使用每日值的平均值:
In [5]: monthly = aapl.resample('1M')
In [6]: monthly.head()
Out[6]:
Open High Low Close Volume Adj Close
Date
1984-09-30 26.981250 27.333125 26.606250 26.738750 4807300.000000 3.007500
1984-10-31 25.035652 25.313478 24.780435 24.806957 5559408.695652 2.788696
1984-11-30 24.545238 24.782857 24.188095 24.236190 5749561.904762 2.724286
1984-12-31 27.060000 27.378500 26.841000 26.947500 6195360.000000 3.031500
1985-01-31 29.520000 29.855909 29.140000 29.253182 10353818.181818 3.289091
绘制每月数据的'Close'列:
In [7]: monthly.plot(y='Close')
Out[7]: <matplotlib.axes.AxesSubplot at 0x45ff4d0>
查看'Close'列的内容:
In [8]: monthly['Close']
Out[8]:
Date
1984-09-30 26.738750
1984-10-31 24.806957
1984-11-30 24.236190
1984-12-31 26.947500
1985-01-31 29.253182
1985-02-28 28.089474
1985-03-31 22.741429
1985-04-30 21.425238
1985-05-31 19.656818
1985-06-30 16.399000
1985-07-31 17.185455
1985-08-31 15.098636
1985-09-30 15.738500
1985-10-31 16.940000
1985-11-30 19.460000
...
2011-12-31 392.930476
2012-01-31 428.578000
2012-02-29 497.571000
2012-03-31 577.507727
2012-04-30 606.003000
2012-05-31 564.673182
2012-06-30 574.562381
2012-07-31 601.068095
2012-08-31 642.696087
2012-09-30 681.568421
2012-10-31 634.714286
2012-11-30 564.345714
2012-12-31 532.055000
2013-01-31 497.822381
2013-02-28 459.026875
Freq: M, Name: Close, Length: 342
这是通过plot
方法生成的图表:
1
你需要按照以下步骤来操作:
首先,你需要把每个元组中第二部分的字符串 ('122.1') 转换成浮点数。你可以使用
float()
这个方法来完成。其次,你需要用
sum()
方法和 列表推导式 来计算所有元组第二部分的总和。最后,用
len()
函数返回的列表长度来进行除法运算。
代码示例:
def average_data(list_of_tuples):
stock_data = [float(t[1]) for t in list_of_tuples]
stock_sum = sum(stock_data)
return stock_sum / len(list_of_tuples)
示例:
list_of_tuples = [('2012-03-24' , '122.10'), ('2012-03-25' , '117.30'), ('2012-03-26' , '126.9')]
print average_data(list_of_tuples)
>>> 122.1