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. 关联对象 AssociatedObject 完全解析

    我们在 iOS 开发中经常需要使用分类(Category),为已经存在的类添加属性的需求,但是使用 @property 并不能在分类中正确创建实例变量和存取方法. 不过,通过 Objective-C ...

  2. css3实现轮播图

    css3动画属性简写: animation: name  duration  timing-function  delay  iteration-count  direction  fill-mode ...

  3. NOIp2018模拟赛四十四

    加量不加价?! 昨晚看时间变成了3.5h以为终于变成了正常难度,结果还是国家集训队作业... A题看起来很神仙,B题看上去很神仙,C题一看就知道很神仙: 结果发现B是假题,放榜后发现A也是假题,C是Y ...

  4. BZOJ 3376 [Usaco2004 Open]Cube Stacking 方块游戏(带权并查集)

    题解 #include<iostream> #include<cstring> #include<cstdio> #include<cmath> #in ...

  5. pandas 3 设置值

    from __future__ import print_function import pandas as pd import numpy as np np.random.seed(1) dates ...

  6. Fiddler 接口测试(Composer)的使用方法

    原文:Fiddler 接口测试(Composer)的使用方法 下载地址:https://www.telerik.com/download/fiddler 一.Composer简介 右侧Composer ...

  7. flume 读取kafka 数据

    本文介绍flume读取kafka数据的方法 代码: /************************************************************************* ...

  8. UVALIVE 4287 Proving Equivalences (强连通分量+缩点)

    题意:给定一个图,问至少加入多少条边能够使这个图强连通. 思路:首先求出这个图的强连通分量.然后把每个强连通分量缩成一个点.那么这个图变成了一个DAG,求出全部点的入度和出度,由于强连通图中每个节点的 ...

  9. Android bluetooth介绍(一):基本概念及硬件接口

    关键词:蓝牙硬件接口 UART  PCM  blueZ 版本号:基于android4.2之前版本号 bluez内核:linux/linux3.08系统:android/android4.1.3.4作者 ...

  10. CSDN博客2014年4月24日清理缓存

    亲爱的CSDN博主们.我们将于今天(2014年4月24日)对CSDN博客频道缓存进行清理,假设您登录后发现自己的文章总数.积分.评论数.訪问数出现异常,请不要慌张.您的数据并没有丢失.将会在缓存清理完 ...