Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
import numpy as np
import pandas as pd
#-- Generate some data similar to yours
idx = np.arange(20)
idx1 = idx[:15]
idx2 = idx[:10]
df1 = pd.DataFrame({'idx':idx1, 'data':np.random.random(idx1.size)})
df2 = pd.DataFrame({'idx':idx2, 'data':np.random.random(idx2.size)})
print df1
print df2
#-- Now let's update df1's values with df2's, based on "idx".
## First let's ensure that the two are indexed on the "idx" field
df1.index, df2.index = df1.idx, df2.idx
# And let's combine df1 and df2, ensuring that df1's values are updated with
# df2's, if hey have the same index
result = df2.combine_first(df1)
print result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.