Create an empty Data frame with date index:

import pandas as pd

def test_run():
start_date='2017-11-24'
end_data='2017-11-28'
dates=pd.date_range(start_date, end_data)
df1=pd.DataFrame(index=dates)
print(df1) """
Empty DataFrame
Columns: []
Index: [2010-01-22 00:00:00, 2010-01-23 00:00:00, 2010-01-24 00:00:00, 2010-01-25 00:00:00, 2010-01-26 00:00:00]
"""

Now we want to load SPY.csv and get 'Adj Close' column value and copy the range (11-21, 11-28) data to the empty data frame:

import pandas as pd

def test_run():
start_date='2017-11-24'
end_data='2017-11-28'
dates=pd.date_range(start_date, end_data) # Create an empty data frame
df1=pd.DataFrame(index=dates) # Load csv file
dspy=pd.read_csv('data/SPY.csv',
index_col="Date",
parse_dates=True,
usecols=['Date', 'Adj Close'],
na_values=['nan'])
# print(dspy)
"""
Adj Close
Date
2017-11-16 258.619995
2017-11-17 257.859985
2017-11-20 258.299988
""" # join the table
df1=df1.join(dspy)
#print(df1)
"""
Adj Close
2017-11-24 260.359985
2017-11-25 NaN
2017-11-26 NaN
2017-11-27 260.230011
""" # drop the nan row
df1=df1.dropna()
print(df1)
"""
Adj Close
2017-11-24 260.359985
2017-11-27 260.230011
2017-11-28 262.869995
""" if __name__ == '__main__':
test_run()

There is a simpy way to drop the data which index is not present in dspy:

df1=df1.join(dspy, how='inner')

We can also rename the 'Adj Close' to prevent conflicts:

    # rename the column
dspy=dspy.rename(columns={'Adj Close': 'SPY'})

Load more stocks:

import pandas as pd

def test_run():
start_date='2017-11-24'
end_data='2017-11-28'
dates=pd.date_range(start_date, end_data) # Create an empty data frame
df1=pd.DataFrame(index=dates) # Load csv file
dspy=pd.read_csv('data/spy.csv',
index_col="Date",
parse_dates=True,
usecols=['Date', 'Adj Close'],
na_values=['nan'])
# print(dspy)
"""
Adj Close
Date
2017-11-16 258.619995
2017-11-17 257.859985
2017-11-20 258.299988
""" # rename the column
dspy=dspy.rename(columns={'Adj Close': 'spy'}) # join the table
df1=df1.join(dspy, how='inner')
# print(df1)
"""
Adj Close
2017-11-24 260.359985
2017-11-27 260.230011
2017-11-28 262.869995
""" symbols=['aapl', 'ibm']
for symbol in symbols:
temp=pd.read_csv('data/{0}.csv'.format(symbol), index_col="Date", parse_dates=True, usecols=['Date', 'Adj Close'], na_values=['nan']) temp=temp.rename(columns={'Adj Close': symbol}) df1=df1.join(temp) print(df1)
"""
spy aapl ibm
2017-11-24 260.359985 174.970001 151.839996
2017-11-27 260.230011 174.089996 151.979996
2017-11-28 262.869995 173.070007 152.470001
""" if __name__ == '__main__':
test_run()

[Python] Pandas load DataFrames的更多相关文章

  1. python & pandas链接mysql数据库

    Python&pandas与mysql连接 1.python 与mysql 连接及操作,直接上代码,简单直接高效: import MySQLdb try: conn = MySQLdb.con ...

  2. Python pandas ERROR 2006 (HY000): MySQL server has gone away

    之前在做python pandas大数据分析的时候,在将分析后的数据存入mysql的时候报ERROR 2006 (HY000): MySQL server has gone away 原因分析:在对百 ...

  3. Python+Pandas 读取Oracle数据库

    Python+Pandas 读取Oracle数据库 import pandas as pd from sqlalchemy import create_engine import cx_Oracle ...

  4. 看到篇博文,用python pandas改写了下

    看到篇博文,https://blog.csdn.net/young2415/article/details/82795688 需求是需要统计部门礼品数量,自己简单绘制了个表格,如下: 大意是,每个部门 ...

  5. Python pandas快速入门

    Python pandas快速入门2017年03月14日 17:17:52 青盏 阅读数:14292 标签: python numpy 数据分析 更多 个人分类: machine learning 来 ...

  6. Python pandas & numpy 笔记

    记性不好,多记录些常用的东西,真·持续更新中::先列出一些常用的网址: 参考了的 莫烦python pandas DOC numpy DOC matplotlib 常用 习惯上我们如此导入: impo ...

  7. python. pandas(series,dataframe,index) method test

    python. pandas(series,dataframe,index,reindex,csv file read and write) method test import pandas as ...

  8. oracle数据据 Python+Pandas 获取Oracle数据库并加入DataFrame

    import pandas as pd import sys import imp imp.reload(sys) from sqlalchemy import create_engine impor ...

  9. Python Pandas找到缺失值的位置

    python pandas判断缺失值一般采用 isnull(),然而生成的却是所有数据的true/false矩阵,对于庞大的数据dataframe,很难一眼看出来哪个数据缺失,一共有多少个缺失数据,缺 ...

随机推荐

  1. (转)String StringBuilder StringBuffer 对比 总结得非常好

    来源:http://blog.csdn.net/clam_clam/article/details/6831345 转自:http://www.iteye.com/topic/522167 作者:每次 ...

  2. C# AES 加解密处理

    引言 这是一个有关AES加解密的方法类 一.设置AES加解密密钥:下面列出自己分配的三类密钥 private const string UserKey = "roshan-2015-user ...

  3. BZOJ 3129 [SDOI2013]方程 (拓展Lucas)

    题目大意:给定一个方程$X_{1}+X_{2}+X_{3}+X_{4}+...+X_{n}=M$,$\forall X_{i}<=A_{i} (i<=n1)$ $\forall X_{i} ...

  4. 紫书 例题 11-1 UVa 12219 (表达式树)

    这道题看了刘汝佳的代码真的是天秀, 很值得学习. 具体看代码 #include<cstdio> #include<iostream> #include<cctype> ...

  5. 编写使用systemctl启动服务脚本

    CentOS 7的服务systemctl脚本存放在:/usr/lib/systemd/,有系统(system)和用户(user)之分,像需要开机不登陆就能运行的程序,还是存在系统服务里吧,即:/usr ...

  6. 【Divide by Zero 2017 and Codeforces Round #399 (Div. 1 + Div. 2, combined) B】 Code For 1

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 把序列生成的过程看成一颗树 会发现最后形成的是一颗二叉树. 每个二叉树上的节点就对应了序列中的一个数字. 如果我们把每个节点都往下投 ...

  7. SpringMVC+Jquery -页面异步载入数据

    背景: 做项目时涉及到页面.当我打算在controller中传一个list到页面,然后通过<c:foreach>循环遍历出来时,同事说:你这样每次都要刷新.这都是几百年前使用的技术了.你用 ...

  8. [MST] Create an Entry Form to Add Models to the State Tree

    It is time to add new entries to the wishlist. We will achieve this by reusing forms and models we'v ...

  9. SpringMVC案例3----spring3.0项目拦截器、ajax、文件上传应用

    依然是项目结构图和所需jar包图: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvYmVuamFtaW5fd2h4/font/5a6L5L2T/fontsi ...

  10. HDOJ 5087 Revenge of LIS II DP

    DP的时候记录下能否够从两个位置转移过来. ... Revenge of LIS II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: ...