[Python] Pandas load DataFrames

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()    
复制代码

 

posted @   Zhentiw  阅读(768)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
历史上的今天:
2016-12-17 [Angular Router] Lazy loading Module with Auxiliary router
2016-12-17 [Now] Configure secrets and environment variables with Zeit’s Now
2015-12-17 [Javascript] Advanced Reduce: Flatten, Flatmap and ReduceRight
2015-12-17 [Redux] Composition with Objects
2014-12-17 [Javascript] Function scope
2014-12-17 [AngularJS] ui-router: named views
2014-12-17 [AngularJS] ui-router: Abstract States
点击右上角即可分享
微信分享提示