Created
September 9, 2015 17:26
-
-
Save joferkington/0d73af8f5ca00ddef997 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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