Combining Two DataFrames with Differing Indexes
You have a dataframe D and have extracted two dataframes A and B from it:
A = D[D.label == k]
B = D[D.label != k]
Your goal is to combine A and B into a single DataFrame, preserving the original order of data from D while retaining the indexes from D.
Solution via Deprecated Method
While DataFrame.append and Series.append are deprecated in v1.4.0, they can still be used for this task with the argument ignore_index set to True. This will discard the original indexes and reindex the combined dataframe from 0 to n-1.
df_merged = df1.append(df2, ignore_index=True)
Solution with Preserved Indexes
If you want to retain the original indexes, set ignore_index to False. This will append the dataframes vertically and retain their respective indexes.
df_merged = df1.append(df2, ignore_index=False)
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3