Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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

Ask Question

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'

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
                I don't think I can explain it better than the documentation, but in essence, you need two pd.DataFrame() to concatenate. Like this: df3 = pd.concat([df1, df2])
– Nicolas Gervais
                Dec 2, 2019 at 4:19

As Nicolas mentioned, concat is a top-level function and doesn't have an equivalent pd.DataFrame method. Other than checking the documentation, you can check if there's a concat method by:

import pandas as pd
hasattr(pd.DataFrame, 'concat')   # False
hasattr(pd, 'concat')             # True

The following are the list of top-level functions that don't have an equivalent pd.DataFrame method:

from inspect import getmembers, isfunction
{n for n,_ in getmembers(pd, isfunction)} - set(dir(pd.DataFrame)) - set(dir(pd.Series))
  • bdate_range, date_range, interval_range, period_range, timedelta_range
  • concat
  • crosstab
  • cut, qcut
  • get_dummies
  • infer_freq
  • json_normalize
  • lreshape
  • merge_asof, merge_ordered
  • read_clipboard, read_csv, read_excel, read_feather, read_fwf, read_gbq, read_hdf, read_html, read_json, read_orc, read_parquet, read_pickle, read_sas, read_spss, read_sql, read_sql_query, read_sql_table, read_stata, read_table, read_xml
  • set_eng_float_format
  • show_versions
  • to_datetime, to_numeric, to_timedelta
  • wide_to_long
  • Thanks for contributing an answer to Stack Overflow!

    • Please be sure to answer the question. Provide details and share your research!

    But avoid …

    • Asking for help, clarification, or responding to other answers.
    • Making statements based on opinion; back them up with references or personal experience.

    To learn more, see our tips on writing great answers.