Df1:
Id val
1 4
3 7
9 2
4 5
Df2:
Id val
1 5
7 2
Required:
Id val
1 5
3 7
9 2
4 5
7 2
I have these df1 and df2 and I want to get the required df where common Ids present in Df1 and Df2 will get updated, and new Ids will get appended.
I dont seem to find if I need to use update, merge or join or something else.
Use concat() to Add a Row at Top of DataFrame concat([new_row,df. loc[:]]). reset_index(drop=True) to add the row to the first position of the DataFrame as Index starts from zero. reset_index() will reset the Index on the DataFrame to adjust the indexes on other rows.
To change the index values we need to use the set_index method which is available in pandas allows specifying the indexes. where, inplace parameter accepts True or False, which specifies that change in index is permanent or temporary. True indicates that change is Permanent.
append() function is used to append rows of other dataframe to the end of the given dataframe, returning a new dataframe object. Columns not in the original dataframes are added as new columns and the new cells are populated with NaN value. Parameters: other : DataFrame or Series/dict-like object, or list of these.
Use concat
with drop_duplicates
(note, order may not be preserved).
pd.concat([df1, df2]).drop_duplicates('Id', keep='last')
Id val
1 3 7
2 9 2
3 4 5
0 1 5
1 7 2
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With