Last active
January 23, 2022 11:47
-
-
Save FurcyPin/5543b20dfdde56260707a620eac84d1b 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
from bigquery_frame import BigQueryBuilder | |
from bigquery_frame.auth import get_bq_client | |
from bigquery_frame import functions as f | |
bigquery = BigQueryBuilder(get_bq_client()) | |
df = bigquery.sql(""" | |
SELECT 1 as id, "Bulbasaur" as name, ["Grass", "Poison"] as types, NULL as other_col | |
UNION ALL | |
SELECT 2 as id, "Ivysaur" as name, ["Grass", "Poison"] as types, NULL as other_col | |
""") | |
df.select("id", "name", "types").createOrReplaceTempView("pokedex") | |
df2 = bigquery.sql("""SELECT * FROM pokedex""")\ | |
.withColumn("nb_types", f.expr("ARRAY_LENGTH(types)"))\ | |
.withColumn("name", f.expr("LOWER(name)"), replace=True) | |
df2.show() | |
# +----+-----------+---------------------+----------+ | |
# | id | name | types | nb_types | | |
# +----+-----------+---------------------+----------+ | |
# | 1 | bulbasaur | ['Grass', 'Poison'] | 2 | | |
# | 2 | ivysaur | ['Grass', 'Poison'] | 2 | | |
# +----+-----------+---------------------+----------+ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment