读取并绘制股票数据
- 读取AAPL csv 数据到DataFrame
import pandas as pd
def test_run():
df = pd.read_csv("data/AAPL.csv")
print df #打印整个DataFrame
print df.head() #打印前五行
print df.tail() #打印最后五行
print df[10:21] #打印10-20行
if __name__ == "__main__":
test_run()
- 计算最高收盘价
#计算symbol各股票的最高收盘价
import pandas as pd
def get_max_close(symobl):
df = pd.read_csv("data/{}.csv".format(symbol))
return df['close'].max()
def test_run():
for symbol in ['AAPL', 'IBM']:
print "max close"
print symbol, get_max_close(symbol)
if __name__ == "__main__":
test_run()
- 计算平均成交量
import pandas as pd
def get_mean_volume(symbol):
df = pd.read_csv("data/{}.csv".format(symbol))
return df['volume'].mean()
def test_run():
for symbol in ['AAPL', 'IBM']
print "mean Volume"
print symbol, get_mean_volume(symbol)
if __name__ == "__main__":
test_run()
- 绘制股价数据
import pandas as pd
import matplotlib.pyplot as plt
def test_run():
df = pd.read_csv("data/AAPL.csv")
print df['Adj Close']
df['Adj Close'].plot
plt.show() #must be called to show plots
if __name__ == "__main__":
test_run()
#这样绘制的图片,坐标轴、title均无标示
#由于csv是反时间顺序,故图也是反的
- 绘制IBM的高股价数据
import pandas as pd
import matplotlib.pyplot as plt
def test_run():
df = pd.read_csv("data/IBM.csv")
#Plot "High" prices for "IBM":
print df['High']
df['High'].plot()
plt.show()
if __name__ == "__main__":
test_run()
- 绘制AAPL的收盘价和调整收盘价
import pandas as pd
import matplotlib.pyplot as plt
def test_run():
df = pd.read_csv("data/AAPL.csv")
df[['Close', 'Adj Close']].plot()
plt.show()
if __name__ == "__main__":
test_run()
pandas DataFrame be like:
image.png
网友评论