25

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'
1
  • What had you done to try to solve this? Commented Dec 2, 2019 at 6:07

2 Answers 2

34

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
Sign up to request clarification or add additional context in comments.

1 Comment

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])
2

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
  • test
  • to_datetime, to_numeric, to_timedelta
  • wide_to_long

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.