I have two dataframes, one that contains some information and a shorter dataframe that contains some amendments I want to make to the first. I have tried something similar to that written below.
for x in df['var']:
if x in df2['var']:
df['var2'] == df2['var2']
For example:
---> df
num_legs num_wings num_specimen_seen
falcon 2 2 10
dog 4 0 2
spider 8 0 1
fish 0 0 8
---> df1
num_legs num_wings num_specimen_seen
dog 4 0 5
spider 8 0 9
---> output
num_legs num_wings num_specimen_seen
falcon 2 2 10
dog 4 0 5
spider 8 0 9
fish 0 0 8
Have you tried df.update()
df.update(df1)
num_legs num_wings num_specimen_seen
falcon 2.0 2.0 10.0
dog 4.0 0.0 5.0
spider 8.0 0.0 9.0
fish 0.0 0.0 8.0
Alternative
import numpy as np
df[df.index.isin(df1.index)]=np.nan
df1.combine_first(df)
How it works
#Find shared index between the two dataframes which are in df
.
df[df.index.isin(df1.index)]
#Make them NaN
df[df.index.isin(df1.index)]=np.nan
#Use [.combine_first][1] to fill them
df1.combine_first(df)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.