import pandas as pd
# pd.Series(data=None,index=None,dtype=None)
import numpy as np
import matplotlib.pyplot as plt
import random
from pylab import mpl
#显示中文字体
mpl.rcParams["font.sans-serif"] = ["SimHei"]
#设置正常显示字符
mpl.rcParams["axes.unicode_minus"] = False
五、Pandas画图
5.1 pandas.DataFrame.plot
# DataFrame.plot (kind='line')
# kind : str,需要绘制图形的种类
# ‘line’ : line plot (default)
# ‘bar’ : vertical bar plot
# ‘barh’ : horizontal bar plot
# ‘hist’ : histogram
# ‘pie’ : pie plot
# ‘scatter’ : scatter plot
5.2 pandas.Series.plot
# 更多细节:https://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.plot.html?highlight=plot#pandas.Series.plot
六、文件读取与存储
6.1 CSV
6.1.1 read_csv
# pandas.read_csv(filepath_or_buffer, sep =',', usecols )
# filepath_or_buffer:文件路径
# sep :分隔符,默认用","隔开
# usecols:指定读取的列名,列表形式
data = pd.read_csv("./2.code/data/stock_day.csv", usecols=['open', 'close'])
data
open | close | |
---|---|---|
2018-02-27 | 23.53 | 24.16 |
2018-02-26 | 22.80 | 23.53 |
2018-02-23 | 22.88 | 22.82 |
2018-02-22 | 22.25 | 22.28 |
2018-02-14 | 21.49 | 21.92 |
... | ... | ... |
2015-03-06 | 13.17 | 14.28 |
2015-03-05 | 12.88 | 13.16 |
2015-03-04 | 12.80 | 12.90 |
2015-03-03 | 12.52 | 12.70 |
2015-03-02 | 12.25 | 12.52 |
643 rows × 2 columns
6.1.2 to_csv
# DataFrame.to_csv(path_or_buf=None, sep=', ’, columns=None, header=True, index=True, mode='w', encoding=None)
# path_or_buf :文件路径
# sep :分隔符,默认用","隔开
# columns :选择需要的列索引
# header :boolean or list of string, default True,是否写进列索引值115
# index:是否写进行索引
# mode:'w':重写, 'a' 追加
# 选取10行数据保存,便于观察数据
data[:10].to_csv("./test.csv", columns=['open'