01-01 Reading and plotting stock

2019-05-11  本文已影响0人  非常暴龙兽

读取并绘制股票数据

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是反时间顺序,故图也是反的
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()
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
上一篇 下一篇

猜你喜欢

热点阅读