Created
December 11, 2015 13:02
-
-
Save blippy/e5769a102dd42be9b9c2 to your computer and use it in GitHub Desktop.
Using pyDatalog to perform a data join
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 pyDatalog import pyDatalog as pdl | |
# set up some basic terms | |
pdl.create_terms('F,G,Q,what,eat') | |
# F G | |
+what('pork', 'meat') | |
+what('lamb', 'meat') | |
+what('peas', 'veg') | |
+what('beans', 'veg') | |
# F Q | |
+eat('pork', 3) | |
+eat('lamb', 4) | |
+eat('peas', 2) | |
+eat('beans', 1) | |
# set up the relationships | |
pdl.create_terms('geat,sumg,qtys') | |
geat(G, Q) <= (what(F, G) & eat(F, Q)) | |
(sumg[G] == sum_(Q, for_each = G)) <= geat(G, Q) | |
qtys(G, Q) <= (sumg[G] == Q) | |
# Now onto the crucial question: how much of each food group did I eat? | |
print(qtys(G, Q)) | |
# If we want to manipulate the data in vanilla Python | |
result = qtys(G, Q).data | |
print(result) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Discussion about the code is available here:
https://mcturra2000.wordpress.com/2015/12/11/using-pydatalog-to-join-data-simply/