Python数据分析(7)----Apple公司股价数据分析

本次实验内容为餐饮订单数据的分析,数据请见:https://pan.baidu.com/s/1tL7FE5lxs-gb6Phf8XRu_Q,文件夹:data_analysis,下面的文件:appl_1980_2014.csv 本次实验主要是对python中的数据进行基本操作。

代码为:

#!/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’中的数据截图为:
在这里插入图片描述

运行结果:
在这里插入图片描述

发布了13 篇原创文章 · 获赞 2 · 访问量 2639

猜你喜欢

转载自blog.csdn.net/weixin_43981621/article/details/104174786