Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace rows in a Pandas df with rows from another df

I have 2 Pandas dfs, A and B. Both have 10 columns and the index 'ID'. Where the IDs of A and B match, I want to replace the rows of B with the rows of A. I have tried to use pd.update, but no success yet. Any help appreciated.

like image 454
Chris Parry Avatar asked Sep 01 '16 09:09

Chris Parry


People also ask

How do I replace a section of a DataFrame with another data frame?

To replace values of a DataFrame with the value of another DataFrame, use the replace() method n Pandas.

How do I get rows from a DataFrame that is in another DataFrame in Python?

To get the specific row of Pandas DataFrame using index, use DataFrame. iloc property and give the index of row in square brackets. DataFrame. iloc returns the row as Series object.

How can I replace all values in a DataFrame with another value?

Suppose that you want to replace multiple values with multiple new values for an individual DataFrame column. In that case, you may use this template: df['column name'] = df['column name']. replace(['1st old value', '2nd old value', ...], ['1st new value', '2nd new value', ...])

What is the method you can use to replace values in a DataFrame?

The replace() method replaces the specified value with another specified value. The replace() method searches the entire DataFrame and replaces every case of the specified value.


1 Answers

below code should do the trick

s1 = pd.Series([5, 1, 'a'])
s2 = pd.Series([6, 2, 'b'])
s3 = pd.Series([7, 3, 'd'])
s4 = pd.Series([8, 4, 'e'])
s5 = pd.Series([9, 5, 'f'])



df1 = pd.DataFrame([list(s1), list(s2),list(s3),list(s4),list(s5)],  columns =  ["A", "B", "C"])

s1 = pd.Series([5, 6, 'p'])
s2 = pd.Series([6, 7, 'q'])
s3 = pd.Series([7, 8, 'r'])
s4 = pd.Series([8, 9, 's'])
s5 = pd.Series([9, 10, 't'])

df2 = pd.DataFrame([list(s1), list(s2),list(s3),list(s4),list(s5)],  columns =  ["A", "B", "C"])

df1.loc[df1.A.isin(df2.A), ['B', 'C']] = df2[['B', 'C']]
print df1

output

   A   B  C
0  5   6  p
1  6   7  q
2  7   8  r
3  8   9  s
4  9  10  t

Edit from comments:

To replace the whole row instead of only some columns:

cols = list(df1.columns) 
df1.loc[df1.A.isin(df2.A), cols] = df2[cols]
like image 200
Shijo Avatar answered Sep 29 '22 05:09

Shijo