[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 ...
随机推荐
- javascript中的正则示例
// 方式一var obj_re = new RegExp("\d+","gi"); //g 全局,i 不区分大小写obj_re.test("fasf ...
- 基本数据类型(list、tuple)
1.列表 1.1 定义 li=[1,2,3] 每个元素逗号隔开 list("abc") 迭代 列表是一个容器 => 任意类型 列表是有序的 => 索引 切片 步长 列表 ...
- js对象追加到数组里
描述:将一个点击事件得到的对象追加到数组里 做法:全局声明一个数组,,在对象的点击事件里将得到的对象追加到数组 change(a){ arr.push(a) console.log(arr) var ...
- JQ 添加节点和插入节点的方法总结
转载来源:http://blog.csdn.net/ss1106404013/article/details/49274345 添加节点的jQuery方法: append().prepend().ap ...
- 洛谷 P1005 矩阵取数游戏 (区间dp+高精度)
这道题大部分时间都在弄高精度-- 还是先讲讲dp吧 这道题是一个区间dp,不过我还是第一次遇到这种类型的区间dp f[i][j]表示取了数之后剩下i到j这个区间的最优值 注意这里是取了i之前和j之后的 ...
- springboot 错误页面的配置
springboot的错误页面,只需在templates下新建error文件夹,将404.500等错误页面放进去即可, springboot会自动去里面查找
- JAVA关于byte数组与String转换的问题
1 public class ToString{ public static void main(String[] args){ String aa = "hellow"; byt ...
- ASP.NET-Razor语法03
ASP.NET MVC中使用Razor语法 @{} @{ string s ="super xiao lizi"; @s; // 里面的这个@代表着在页面上输出这个s // 我记 ...
- CodeForces 453A
Twilight Sparkle was playing Ludo with her friends Rainbow Dash, Apple Jack and Flutter Shy. But she ...
- C - The C Answer (2nd Edition) - Exercise 1-12
/* Write a program that prints its input one word per line. */ #include <stdio.h> #define IN 1 ...