Pandas Update and Add Rows One Dataframe With Key Column in Another Dataframe

Pandas update and add rows one dataframe with key column in another dataframe

It seems like you're looking for combine_first.

a = df2.set_index('key')
b = df1.set_index('key')

(a.combine_first(b)
.reset_index()
.reindex(columns=df1.columns))

A B key C
0 4.0 5.0 k1 2.0
1 1.0 2.0 k2 3.0
2 2.0 3.0 k3 5.0
3 2.0 3.0 k4 5.0

How to add ROW of one dataframe to column of another dataframe?

You can extract the values from the row, use np.flip() to reverse them and then create a new column.

import numpy as np

df2['CDF'] = np.flip(df1.loc['CDF'].values)

pandas: append rows to another dataframe under the similar row based on column condition

First idea is filter df2 values by df1.col1 and append to df1 by concat and then sorting by DataFrame.sort_values:

df = pd.concat([df1, df2[(df2.col1.isin(df1.col1))]]).sort_values('col1', ignore_index=True)
print (df)
col1 col2 col3
0 I ate dinner min min
1 I ate dinner max max
2 I ate dinner min max
3 the play was inetresting mid max
4 the play was inetresting min max
5 the play was inetresting max mid

If need only common values in both DataFrames is possible filter by numpy.intersect1d:

common = np.intersect1d(df1['col1'], df2['col1'])

df = (pd.concat([df1[df1.col1.isin(common)],
df2[df2.col1.isin(common)]])
.sort_values('col1', ignore_index=True))
print (df)

Updating a dataframe rows based on another dataframe rows

you can use update...like so..

df1.update(df2)

How to update one pandas dataframe with another dataframe (update the old data and add new data)

Use concat with DataFrame.drop_duplicates:

df = pd.concat([df2, df1]).drop_duplicates(subset=['key'])
print (df)
assignee id issuetype key
0 Tom 1 story TP-1
1 Anna 2 bug TP-2
2 Tim 3 bug TP-3
3 Jane 4 bug TP-4

Or DataFrame.update with DataFrame.reindex:

cols = df1.columns
df1 = df1.set_index('key')
df2 = df2.set_index('key')
df1 = df1.reindex(columns=df1.columns.union(df2.columns, sort=False),
index=df1.index.union(df2.index, sort=False))

df1.update(df2)
df1 = df1.reset_index().reindex(columns=cols)
print (df1)

assignee id issuetype key
0 Tom 1.0 story TP-1
1 Anna 2.0 bug TP-2
2 Tim 3.0 bug TP-3
3 Jane 4.0 bug TP-4

Python Pandas update a dataframe value from another dataframe

You can using concat + drop_duplicates which updates the common rows and adds the new rows in df2

pd.concat([df1,df2]).drop_duplicates(['Code','Name'],keep='last').sort_values('Code')
Out[1280]:
Code Name Value
0 1 Company1 200
0 2 Company2 1000
2 3 Company3 400

Update due to below comments

df1.set_index(['Code', 'Name'], inplace=True)

df1.update(df2.set_index(['Code', 'Name']))

df1.reset_index(drop=True, inplace=True)

pandas update specific rows in specific columns in one dataframe based on another dataframe

Use DataFrame.update by ID converted to index and selecting columns for processing - here only age and city:

df11 = df1.set_index('ID')
df22 = df2.set_index('ID')[['age','city']]
df11.update(df22)
df = df11.reset_index()
print (df)
ID name country city hobby age
0 12 Meli Peru Lima eating 24.0
1 15 Saya USA new-york drinking 34.0
2 34 Aitel Jordan Amman riding 51.0
3 23 Tanya Russia Moscow sports 75.0
4 44 Gil Spain Barcelona paella 21.0


Related Topics



Leave a reply



Submit