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
Answer from Nicolas Gervais on Stack OverflowYou 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
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_rangeconcatcrosstabcut,qcutget_dummiesinfer_freqjson_normalizelreshapemerge_asof,merge_orderedread_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_xmlset_eng_float_formatshow_versionstestto_datetime,to_numeric,to_timedeltawide_to_long
Need help on using .append and .concat
Opinions on adding to DataFrames
'DataFrame' object has no attribute 'concat'
Help with: AttributeError: 'DataFrame' object has no attribute 'account_value
Not new to python, but not experienced working with pandas either. This article gives three methods of adding to a dataframe ->
df.loc[len(df.index)] = new_object df.append(new_object) pandas.concat([df1, df2])
I understand that concat is for adding two dataframes together, but is there any difference between df.loc and df.append? Is one preferred in certain situations? Or are they functionally equivalent?