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. 紫书 例题11-8 UVa 11082(网络流最大流)

    这道题的建模真的非常的秀, 非常牛逼. 先讲建模过程.源点到每一行连一条弧, 容量为这一行的和减去列数, 然后每一列到汇点连一条弧, 容量为这一列 的和减去行数, 然后每一行和列之间连一条弧, 容量为 ...

  2. Android Studio生成apk

    1.菜单Build->Generate Signed APK 2.生成android.keystore,能够依据弹框去Create new一个,也可使用命令来生成android.keystore ...

  3. <九度 OJ>题目1545:奇怪的连通图

    题目描写叙述: 已知一个无向带权图,求最小整数k.使仅使用权值小于等于k的边,节点1能够与节点n连通. 输入: 输入包括多组測试用例.每组測试用例的开头为一个整数n(1 <= n <= 1 ...

  4. [leetcode][math] Add Digits

    题目: Given a non-negative integer num, repeatedly add all its digits until the result has only one di ...

  5. 可靠的Windows版Redis

    副标题: 评论更精彩,教你怎么解决64位Windows版Redis狂占C盘的问题. MS Open Tech 技术团队近期花了非常多时间来測试最新构建的Windows版Redis(可在 MS Open ...

  6. SparkCore基础(一)

    * SparkCore基础(一) 学习Spark,首先要熟悉Scala,当然你说你会Python或者Java能不能玩Spark?能!但是不推荐,首推Scala,因为Scala非常便捷,而且Scala有 ...

  7. Spring MVC登录注册以及转换json数据

    项目结构; 代码如下: BookController package com.mstf.controller; import javax.servlet.http.HttpServletRespons ...

  8. .NET简谈——跨进高级编程门槛的必经之路

    我们继续C#基础知识的学习,这篇文章对前面基础知识学习的朋友有着举足轻重的作用:为了延续基础知识学习的热情,我编写了这篇特殊的文章. 本篇文章的中心是想借“.NET简谈反射(动态调用)”一文继续发挥下 ...

  9. 关于linq使用建议

    1.由于目前我做的项目使用的是SocanCode工具生成的三层框架,于是乎对于一些基本数据类型采用了可空类型编程 public CountryCode(int? id, string en_name, ...

  10. <Sicily>Pythagorean Proposition

    一.题目描述 One day, WXYZ got a wooden stick, he wanted to split it into three sticks and make a right-an ...