[Python] Pandas Load Dataframes

Source: Internet
Author: User

Create an empty Data frame with date index:

ImportPandas as PDdefTest_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 dataframecolumns: []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 of the range (11-21, 11-28) data to the empty data fram E:

ImportPandas as PDdefTest_run (): start_date='2017-11-24'End_data='2017-11-28'dates=Pd.date_range (start_date, End_data)#Create an empty data frameDF1=PD. DataFrame (index=dates)#Load csv fileDspy=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 tabledf1=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 rowdf1=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-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:

ImportPandas as PDdefTest_run (): start_date='2017-11-24'End_data='2017-11-28'dates=Pd.date_range (start_date, End_data)#Create an empty data frameDF1=PD. DataFrame (index=dates)#Load csv fileDspy=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 columnDspy=dspy.rename (columns={'ADJ Close':'Spy'})    #Join the tableDf1=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']     forSymbolinchsymbols: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 26 0.230011 174.089996 151.979996 2017-11-28 262.869995 173.070007 152.470001"""if __name__=='__main__': Test_run ()

[Python] Pandas Load Dataframes

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.