Search code examples
pythonpython-3.xpandasdataframeconcatenation

Concat 2 columns in pandas - AttributeError: 'DataFrame' object has no attribute 'concat'


I am trying to concatenate along 2 columns in pandas. The code :

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(2015,2,12)
end = datetime.datetime.today()
df = web.get_data_yahoo(['F', '^GSPC'], start, end)

df1 = df.concat(columns=[F['Close'], gspc['Close']], axis=1)

But I am getting the following error:

AttributeError: 'DataFrame' object has no attribute 'concat'

Solution

  • You need to use pd.concat([df1, df2]), because df.concat() doesn't exist.

    I'll make you an example:

    import pandas as pd
    
    df1 = pd.DataFrame(zip(list('bcdfg'), list('aeiou')), columns=['consonants', 'vowels'])
    df2 = pd.DataFrame(range(5), columns=['numbers'])
    
      consonants vowels
    0          b      a
    1          c      e
    2          d      i
    3          f      o
    4          g      u
    
       numbers
    0        0
    1        1
    2        2
    3        3
    4        4
    
    pd.concat([df1, df2], axis=1)
    
      consonants vowels  numbers
    0          b      a        0
    1          c      e        1
    2          d      i        2
    3          f      o        3
    4          g      u        4