Skip to content

Instantly share code, notes, and snippets.

@frankie-yanfeng
Created April 17, 2019 10:03
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 frankie-yanfeng/2186ce692787f9447d6c667cfd93556f to your computer and use it in GitHub Desktop.
Save frankie-yanfeng/2186ce692787f9447d6c667cfd93556f to your computer and use it in GitHub Desktop.
SQL python function
# 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