Need help on using .append and .concat

Here is my coding:

train_dataset = pd.read_csv('train.csv')

test_dataset = pd.read_csv('test.csv')

train_dataset.head()

test['Target'] = np.nan

data = train_dataset.append(test_dataset, ignore_index = True)

-------------------------------------------------------------------------------------------------------
The system prompts me this message:
/var/folders/7y/rvhwt04n48g8nsfq443cymkh0000gn/T/ipykernel_5081/342987204.py:11: FutureWarning: The frame.append method is deprecated and will be removed from pandas in a future version. Use pandas.concat instead.
  data = train.append(test, ignore_index = True)

I replace .append with .concat but it throws me errors:
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Cell In[35], line 11
      7 train.head()
      9 test['Target'] = np.nan
---> 11 data = train.concat(test, ignore_index = True)
     13 head_of_household = data.loc[data['parentesco1'] == 1].copy()
     15 train_labels = data.loc[(data['Target'].notnull()) & (data['parentesco1'])]

File ~/anaconda3/lib/python3.11/site-packages/pandas/core/generic.py:5902, in NDFrame.__getattr__(self, name)
   5895 if (
   5896     name not in self._internal_names_set
   5897     and name not in self._metadata
   5898     and name not in self._accessors
   5899     and self._info_axis._can_hold_identifiers_and_holds_name(name)
   5900 ):
   5901     return self[name]
-> 5902 return object.__getattribute__(self, name)

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

Why this is an attribute error?  Why can't I replace .append with .concat?  Do I need to use a new library?  What did I miss?

Thanks.

The AttributeError is occurring because the DataFrame object in pandas does not have an attribute named concat. Instead, you should use the pd.concat() function to concatenate DataFrames.You can replace the .append() method with pd.concat() like this:

data = pd.concat([train_dataset, test_dataset], ignore_index=True)

Pandas docs

GeeksForGeeks tutorial

1 Like

Thank you!

1 Like