赞
踩
代码为:
#!/usr/bin/env python # -*- coding:utf-8 -*- import pandas as pd import matplotlib.pyplot as plt # 1. 读取数据并存为一个名叫 apple 的数据框。 apple = pd.read_csv('data_analysis/appl_1980_2014.csv') # 2. 查看每一列的数据类型。 apple.dtypes # 3. 将 Date 这个列转换为 datetime 类型。 # 此处是运用了pandas的to_datetime函数,其中format是表示的原始的数据‘Data’的格式,Y、m、d分别表示年、月、日 # to_datetime函数可以将任意包含日期格式的字符串转换成datetime类型,一定要注意format的表示方式 apple['Date'] = pd.to_datetime(apple['Date'], format='%Y-%m-%d') # 4. 将 Date 设置为索引。 apple.set_index('Date', inplace=True) # 5. 有重复的日期吗? a = apple.groupby('Date').count() > 1 a.shape b = apple.Date.duplicated() b.sum() apple.index.is_unique apple.shape[0] == apple.index.nunique() # 6. 将 index 设置为升序。 apple = apple.sort_index(ascending=True) # 7. 找到每个月的最后一个交易日(businessday)。 # 这里用到了pandas里的重采样函数resample,常用的重采样的频率:(除下面列出的外,也可以用其他常用的频率) # D calendar day frequency # W weekly frequency # M month end frequency # H hourly frequency # T minutely frequency # S secondly frequency # 需要注意的是,resample函数中有个参数为label,默认取的是最大值 apple_m = apple.resample('M').mean() print(apple_m.head()) # 先进行分组,然后找到分组里面索引的最大值 apple.groupby([apple.index.year, apple.index.month]).agg({'Open':lambda x: x.index.max()}) # 8. 数据集中最早的日期和最晚的日期相差多少天? (apple.index.max()-apple.index.min()).days # 9. 在数据中一共有多少个月? (apple.index.max()-apple.index.min()).days/30 len(apple_m) apple.groupby([apple.index.year, apple.index.month]).agg({'Open':lambda x: x.index.max()}).shape[0] # 10. 按照时间顺序可视化 Adj Close 值。 apple['Adj Close'].plot(title='Apple Stock').get_figure().set_size_inches(9, 5) y = apple['Adj Close'].sort_index() plt.plot(y.index, y)
文件‘appl_1980_2014.csv’中的数据截图为:
运行结果:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。