简体   繁体   English

如果列表中存在,则根据另一列的值选择列

[英]Select columns based on values of another column if present in a list

I'm trying to select columns A and B from my table in Bigquery using pandas.read_gbq based on values of column C if present in a list. 我试图根据列中的C列值使用pandas.read_gbq从Bigquery的表中选择A列和B列(如果存在于列表中)。 However, when I use format to insert the list in my query string, the contents of the list are surrounded by [] square brackets. 但是,当我使用format在查询字符串中插入列表时,列表的内容用[]方括号括起来。 This breaks my query. 这打断了我的查询。

I used replace on the query string to manually remove the square brackets. 我在查询字符串上使用了replace来手动删除方括号。

values_in_list = ['a', 'b', 'c']
query = """
SELECT
  column_A,
  column_B

FROM
  my_table

WHERE
 column_C IN ({})
""".format(values_in_list).replace('[', '').replace(']', '')
query_df = pandas.read_gbq(query, project_id='some-project', dialect='standard')

This gets the job done. 这样就完成了工作。 I was wondering if there was a more elegant solution than brute forcing it. 我想知道是否有比强行强制解决方案更优雅的解决方案。

I'm not sure if pandas.read_gbq supports ArrayQueryParameters in the query_config keyword arg. 我不确定pandas.read_gbq ArrayQueryParametersquery_config关键字arg中支持ArrayQueryParameters Here's my workaround: 这是我的解决方法:

from google.cloud import bigquery
client = bigquery.Client()

values_in_list = ['a', 'b', 'c']
query = """
SELECT
  column_A,
  column_B

FROM
  my_table

WHERE
 column_C IN UNNEST(@col_c_vals)
"""

query_params = [bigquery.ArrayQueryParameter('col_c_vals', 'STRING', values_in_list)]
job_config = bigquery.QueryJobConfig()
job_config.query_parameters = query_params
query_df = client.query(query, job_config=job_config).to_dataframe()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM