[Python] Normalize the data with Pandas

來源:互聯網
上載者:User

標籤:技術   es2017   nan   otl   run   blog   style   import   star   

import osimport pandas as pdimport matplotlib.pyplot as pltdef test_run():    start_date=‘2017-01-01‘    end_data=‘2017-12-15‘    dates=pd.date_range(start_date, end_data)    # Create an empty data frame    df=pd.DataFrame(index=dates)    symbols=[‘SPY‘, ‘AAPL‘, ‘IBM‘, ‘GOOG‘, ‘GLD‘]    for symbol in symbols:        temp=getAdjCloseForSymbol(symbol)        df=df.join(temp, how=‘inner‘)    return df   def normalize_data(df):    """ Normalize stock prices using the first row of the dataframe """    df=df/df.ix[0, :]    return dfdef getAdjCloseForSymbol(symbol):     # Load csv file    temp=pd.read_csv("data/{0}.csv".format(symbol),         index_col="Date",         parse_dates=True,        usecols=[‘Date‘, ‘Adj Close‘],        na_values=[‘nan‘])    # rename the column    temp=temp.rename(columns={‘Adj Close‘: symbol})    return tempdef plot_data(df, title="Stock prices"):    ax=df.plot(title=title, fontsize=10)    ax.set_xlabel("Date")    ax.set_ylabel("Price")    plt.show()if __name__ == ‘__main__‘:    df=test_run()    # data=data.ix[‘2017-12-01‘:‘2017-12-15‘, [‘IBM‘, ‘GOOG‘]]        df=normalize_data(df)    plot_data(df)    """                       IBM         GOOG    2017-12-01  154.759995  1010.169983    2017-12-04  156.460007   998.679993    2017-12-05  155.350006  1005.150024    2017-12-06  154.100006  1018.380005    2017-12-07  153.570007  1030.930054    2017-12-08  154.809998  1037.050049    2017-12-11  155.410004  1041.099976    2017-12-12  156.740005  1040.479980    2017-12-13  153.910004  1040.609985    2017-12-15  152.500000  1064.189941    """

It is easy to compare the data by normalize it.

 

[Python] Normalize the data with Pandas

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.