探索Apple公司股價數(shù)據(jù)
數(shù)據(jù)源:鏈接: https://pan.baidu.com/s/1EFqJFXf70t2Rubkh6D19aw 提取碼: syqg
數(shù)據(jù)源示例:

步驟1 導入必要的庫
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
步驟2 數(shù)據(jù)集地址
path1='pandas_exercise\exercise_data\Apple_stock.csv'
步驟3 讀取數(shù)據(jù)并存為一個名叫apple的數(shù)據(jù)框
apple=pd.read_csv(path1)
print(apple.head())
步驟4 查看每一列的數(shù)據(jù)類型
print(apple.dtypes)
步驟5 將Date這個列轉(zhuǎn)換為datetime類型
apple.Date=pd.to_datetime(apple.Date)
print(apple.dtypes)
步驟6 將Date設置為索引
apple=apple.set_index(apple.Date)
print(apple.head())
步驟7 有重復的日期嗎?
print(apple.index.is_unique)
步驟8 將index設置為升序
apple.sort_index(ascending=True).head()
步驟9 找到每個月的最后一個交易日(business day)
apple_month=apple.resample('BM')
步驟10 數(shù)據(jù)集中最早的日期和最晚的日期相差多少天?
print((apple.Date.max()-apple.Date.min()).days)
步驟11 在數(shù)據(jù)中一共有多少個月?
apple_months=apple.resample('BM').mean()
print(len(apple_months.index))
步驟12 按照時間順序可視化Adj Close值
apple['Adj Close'].plot(title='Apple Stock')
plt.show()
