[Python] Read and plot data from csv file

Install:

pip install pandas
pip install matplotlib # check out the doc from site

 

import pandas as pd 
import matplotlib.pyplot as plt
from numpy import mean

def load_df(symbol):
    return pd.read_csv("data/{0}.csv".format(symbol))

def get_max_close(symbol):
    """ Return the maximum closing value for stock idicicated by symbol.
    Note: Data for a stock is stored in file: data/<symbol>.csv
    """
    df = load_df(symbol)
    return df["Close"].max()

def get_mean_volume(symbol):
    df = load_df(symbol)
    return mean(df['Volume'])

def printAdjClose():
    df = pd.read_csv('data/ibm.csv')
    df[['Low', 'High']].plot()
    plt.show()

def test_run():
    """
    Function called by Test Run
    """
    for symbol in ['aapl', 'ibm']:
        print("Max close")
        print(symbol, get_max_close(symbol))
        print("Mean Volume")
        print(symbol, get_mean_volume(symbol))

if __name__ == "__main__":
    test_run()
    printAdjClose()

 

posted @ 2017-12-15 20:58  Zhentiw  阅读(354)  评论(0编辑  收藏  举报