Skip to content

Instantly share code, notes, and snippets.

@joferkington
Created September 9, 2015 17:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joferkington/0d73af8f5ca00ddef997 to your computer and use it in GitHub Desktop.
Save joferkington/0d73af8f5ca00ddef997 to your computer and use it in GitHub Desktop.
import numpy as np
import pandas as pd
#-- Generate some data similar to yours
idx = np.arange(20)
np.random.shuffle(idx)
idx1 = idx[:15]
np.random.shuffle(idx)
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