当前位置:   article > 正文

Python数据分析(7)----Apple公司股价数据分析_请编写python代码,对appl公司股价数据进行下列操作: (1)读取数据文件appl.csv,储

请编写python代码,对appl公司股价数据进行下列操作: (1)读取数据文件appl.csv,储

本次实验内容为餐饮订单数据的分析,数据请见: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)


  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55

文件‘appl_1980_2014.csv’中的数据截图为:
在这里插入图片描述

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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/142167
推荐阅读
相关标签
  

闽ICP备14008679号