Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update dataframe based on index and append the new ones

Tags:

python

pandas

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.

like image 964
star_kid Avatar asked Oct 22 '18 18:10

star_kid


People also ask

How do I add a new row to a DataFrame with a new index?

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.

How do you update a DataFrame index?

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.

How do I append a DataFrame to an existing data frame?

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.


1 Answers

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
like image 175
cs95 Avatar answered Sep 29 '22 11:09

cs95