Created
April 17, 2019 10:03
-
-
Save frankie-yanfeng/2186ce692787f9447d6c667cfd93556f to your computer and use it in GitHub Desktop.
SQL python function
This file contains 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
# Create function that finds the number of records and the average weight for each value of the chosen column | |
def get_distinct_values(column_name): | |
sql = """ | |
SELECT | |
{0}, | |
COUNT(1) AS num_babies, | |
AVG(weight_pounds) AS avg_wt | |
FROM | |
publicdata.samples.natality | |
WHERE | |
year > 2000 | |
GROUP BY | |
{0} | |
""".format(column_name) | |
return bq.Query(sql).execute().result().to_dataframe() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment