0

I have two dataframes

df_train_1 with shape (70652, 4)

and

df_test_1 with shape (24581, 4)

I am trying to concat them with df_train_1 ontop.

I have tried the following two methods:

df_combined = df_train_1.append(df_test_1)


df_combined = pd.concat([df_train_1, df_test_1])

when I call df_combined.title[0] I get the both [0] values from the original dataframe. Can someone point me in the direction of how to avoid this please

unaied
  • 197
  • 11

1 Answers1

1

If you look at the example of the documentation of pandas: https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.append.html

df = pd.DataFrame([[1, 2], [3, 4]], columns=list('AB'))
df
   A  B
0  1  2
1  3  4
df2 = pd.DataFrame([[5, 6], [7, 8]], columns=list('AB'))
df.append(df2)
   A  B
0  1  2
1  3  4
0  5  6
1  7  8

You will see that the index will be stacked.

So like the comment suggested, use ignore_index=True to reset the index to numeric order:

df.append(df2, ignore_index=True)
   A  B
0  1  2
1  3  4
2  5  6
3  7  8
Cheukting
  • 264
  • 1
  • 6