Created
April 9, 2015 23:50
-
-
Save joefutrelle/9f4e9193952fcb0da493 to your computer and use it in GitHub Desktop.
pandas merge example
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
| from pandas import DataFrame, merge | |
| import numpy as np | |
| # generate array of random small integers | |
| df1 = DataFrame(np.random.random_integers(5,size=(5,2)),columns=['a','b']) | |
| # find unique values | |
| uq = df1.a.unique() | |
| # now generate two associated columns per unique value | |
| df2 = DataFrame({ | |
| 'a': uq, | |
| 'bar': np.random.randn(uq.size), | |
| 'baz': np.random.randn(uq.size) | |
| }) | |
| print df1 | |
| print df2 | |
| # merge on the shared column | |
| print merge(df1, df2, on='a') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment