Search code examples
python-3.xpandaspandas-datareader

Concatenated data from pandas_datareader


I am trying to create a dataframe which columns from 2 different datarame.

import pandas as pd
import numpy as np
from statsmodels import api as sm
import pandas_datareader.data as web
import datetime
start = datetime.datetime(2016,12,2)
end = datetime.datetime.today()
df = web.get_data_yahoo(['F'], start, end)
df1 = web.get_data_yahoo(['^GSPC'], start, end)
df3 = pd.concat([df['Adj Close'], df1['Adj Close']])

With this i wanted to get df3 with 2 columns containing data of [Adj Close]. What i got instead is :

    F   ^GSPC
Date        
2016-12-01  10.297861   NaN
2016-12-02  10.140451   NaN
2016-12-05  10.306145   NaN
2016-12-06  10.405562   NaN
2016-12-07  10.819797   NaN
... ... ...
2019-11-22  NaN 3110.290039
2019-11-25  NaN 3133.639893
2019-11-26  NaN 3140.520020
2019-11-27  NaN 3153.629883
2019-11-29  NaN 3140.979980
1508 rows × 2 columns

What do i need to do to get rid of NaN values and why is it there?


Solution

  • Add parameter axis=1 for concanecate by columns in concat:

    df3 = pd.concat([df['Adj Close'], df1['Adj Close']], axis=1)
    

    But I think your solution should be simplify with pass list to get_data_yahoo:

    df3 = web.get_data_yahoo(['F', '^GSPC'], start, end)