5

I have two dataframes with identical structures df and df_a. df_a is a subset of df that I need to reintegrate into df. Essentially, df_a has various rows (with varying indices) from df that have been manipulated.

Below is an example of indices of each df and df_a. These both have the same column structure so all the columns are the same, it's only the rows and idex of the rows that differ.

>> df
index  ..  other_columns  ..
0   
1
2
3
. .
9999
10000
10001

[10001 rows x 20 columns]

>> df_a
index  ..  other_columns  ..
5
12
105
712
. .
9824
9901
9997

[782 rows x 20 columns]

So, I want to overwrite only the rows in df that have the indices of df_a with the corresponding rows in df_a. I checked out Replace rows in a Pandas df with rows from another df and replace rows in a pandas data frame but neither of those tell how to use the indices of another dataframe to replace the values in the rows.

DrakeMurdoch
  • 765
  • 11
  • 26

2 Answers2

5

Something along the lines of:

df.loc[df_a.index, :] = df_a[:]
Kyle
  • 2,814
  • 2
  • 17
  • 30
0

I don't know if this wants you meant, for that you would need to be more specific, but if the first data frame was modified to be a new data frame with different indexes, then you can use this code to reset back the indexes:

import pandas as pd

df_a = pd.DataFrame({'a':[1,2,3,4],'b':[5,4,2,7]}, index=[2,55,62,74])
df_a.reset_index(inplace=True, drop=True)
print(df_a)

PRINTS:
   a  b
0  1  5
1  2  4
2  3  2
3  4  7