简体   繁体   中英

Issue concating two dataframes ontop of each other

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM