[Python] Normalize the data with Pandas
import os
import pandas as pd
import matplotlib.pyplot as plt def 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 df def 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 temp def 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的更多相关文章
- [Python] Slice the data with pandas
For example we have dataframe like this: SPY AAPL IBM GOOG GLD 2017-01-03 222.073914 114.311760 160. ...
- 一句Python,一句R︱pandas模块——高级版data.frame
先学了R,最近刚刚上手python,所以想着将python和R结合起来互相对比来更好理解python.最好就是一句python,对应写一句R. pandas可谓如雷贯耳,数据处理神器. 以下符号: = ...
- Seven Python Tools All Data Scientists Should Know How to Use
Seven Python Tools All Data Scientists Should Know How to Use If you’re an aspiring data scientist, ...
- arcgis python arcpy add data script添加数据脚本
arcgis python arcpy add data script添加数据脚本mxd = arcpy.mapping.MapDocument("CURRENT")... df ...
- [Machine Learning with Python] My First Data Preprocessing Pipeline with Titanic Dataset
The Dataset was acquired from https://www.kaggle.com/c/titanic For data preprocessing, I firstly def ...
- Python数据科学安装Numby,pandas,scipy,matpotlib等(IPython安装pandas)
Python数据科学安装Numby,pandas,scipy,matpotlib等(IPython安装pandas) 如果还没有本地安装Python.IPython.notebook等请移步 上篇Py ...
- 用pandas进行数据清洗(二)(Data Analysis Pandas Data Munging/Wrangling)
在<用pandas进行数据清洗(一)(Data Analysis Pandas Data Munging/Wrangling)>中,我们介绍了数据清洗经常用到的一些pandas命令. 接下 ...
- Python For Data Analysis -- Pandas
首先pandas的作者就是这本书的作者 对于Numpy,我们处理的对象是矩阵 pandas是基于numpy进行封装的,pandas的处理对象是二维表(tabular, spreadsheet-like ...
- Python 数据处理扩展包: pandas 模块的DataFrame介绍(创建和基本操作)
DataFrame是Pandas中的一个表结构的数据结构,包括三部分信息,表头(列的名称),表的内容(二维矩阵),索引(每行一个唯一的标记). 一.DataFrame的创建 有多种方式可以创建Data ...
随机推荐
- js 关于height()、innerHeight()、outerHeight()函数的区别
- Reactive programming-文章解析
数据源(信息源):静态的数组.动态的流: In computing, reactive programming is a declarative programming paradigm concer ...
- 优动漫PAINT-凌霄花画法
再见小清新~这次教程教授的是凌霄花的画法!话说这个作者的花卉系列都很米粒啊~配色什么的,赞到没话说~ 教程是简单,呃.... 没有优动漫PAINT软件肿么办? 别着急,╭(╯^╰)╮ 小编给你送来了 ...
- 3ds Max实例教程-顽皮的小孩
本教程介绍使用3ds Max制作设计一个顽皮的小孩,这个作品的灵感来源于作者的亲身经历,也是以真实人物为原型做出来这么一个小人. 作者: Claudius Vesting 使用软件:3ds Max,P ...
- Python 批量处理特定格式文件
#批量对文件夹下的'.mat'进行处理 def file_name(file_dir,suff): L=[] for root, dirs, files in os.walk(file_dir): f ...
- Django REST framework 自定义字段
自定义字段 继承 Field 类 覆盖父类中的方法 to_representation() 调用该方法将初始数据类型转换为基本的可序列化数据类型 to_internal_value() 调用该方法将原 ...
- angular7升级到angular8
1.首先我们对:angular的命令的安装 ng install -g @angular/cli的安装则会升级到最新的版本,并且再次创建项目的时候,我们就能够使用ng version查看到已经是最新的 ...
- [ZJOI2007]捉迷藏 (点分树+堆*3)
点分树一点都不会啊(还是太菜了) 点分树就是我们点分治构成的新树.满足深度很小. 然后我们就可以在上面瞎维护东西了. 三个大根堆: \(C[u]\)里装的是点分树中u的子树所有点到点分树中u的父亲的距 ...
- dropload上拉加载 下拉刷新
1.引入css.js <link rel="stylesheet" href=" ${pageContext.request.contextPath}/dist/d ...
- 《Craking the Coding interview》python实现---02
###题目:翻转一个字符串###思路:从字符串的最后一位开始,依次取###实现:伪代码.函数.类实现#伪代码: #01string=sNew_s=""for i in range( ...