【发布时间】:2021-02-28 17:48:41
【问题描述】:
【问题讨论】:
-
请发数据,不要发图片
-
由于没有有用的数据,请查看 stack/unstack 和 pd.pivot_table()
标签: python pandas google-bigquery pivot transpose
【问题讨论】:
标签: python pandas google-bigquery pivot transpose
以下是 BigQuery 标准 SQL
execute immediate (
with types as (
select
array_to_string(types, ',') values_list,
regexp_replace(array_to_string(types, ','), r'([^,]+)', r'"\1"') columns_list
from (
select regexp_extract_all(to_json_string(t), r'"([^""]+)":') types
from (
select * except(Country, Branch, Category)
from `project.dataset.your_table` limit 1
) t
)
), categories as (
select distinct Category
from `project.dataset.your_table`
)
select '''
select Country, Branch, Output, ''' ||
(select string_agg('''
max(if(Category = "''' || Category || '''", val, null)) as ''' || Category )
from categories)
|| '''
from (
select Country, Branch, Category,
type[offset(offset)] Output, val
from `project.dataset.your_table` t,
unnest([''' || values_list || ''']) val with offset,
unnest([struct([''' || columns_list || '''] as type)])
)
group by Country, Branch, Output
'''
from types
);
如果应用于您问题中的样本数据 - 输出是
【讨论】: