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
Tkinter Ttk Treeview How to Set Fixed Width Why It Change With Number of Column
How to Change the Foreground or Background Colour of a Tkinter Button on MAC Os X
Extract Files from Zip File and Retain Mod Date
How to Avoid Last Comma in Python Loop
Cv2 Image Error: Error: (-215:Assertion Failed) !Ssize.Empty() in Function 'Cv::Resize'
Modulenotfounderror: What Does It Mean _Main_ Is Not a Package
Python: Pickle.Load() Raising Eoferror
Python Anaconda - How to Safely Uninstall
Pythonic Way to Check If Two Dictionaries Have the Identical Set of Keys
Python: How to Calculate the Sum of Numbers from a File
Python3 Tkinter Set Image Size
Split Data Directory into Training and Test Directory With Sub Directory Structure Preserved
How to Iterate Through Cur.Fetchall() in Python
How to Count the Number of Files in a Directory Using Python
Python-Compare Two String Columns in Same Dataframe, Return Matching Result