【问题标题】:How to do pivot operation on multiple columns on python pandas or big query. Preferably on big query如何在 python pandas 或大查询上对多个列进行枢轴操作。最好在大查询上
【发布时间】:2021-02-28 17:48:41
【问题描述】:

原始数据:

所需转换后数据的外观:

我在 python pandas 中尝试过 melt 功能,但我只能在一列上旋转。我确定我一定错过了什么。

【问题讨论】:

  • 请发数据,不要发图片
  • 由于没有有用的数据,请查看 stack/unstack 和 pd.pivot_table()

标签: python pandas google-bigquery pivot transpose


【解决方案1】:

以下是 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
);   

如果应用于您问题中的样本数据 - 输出是

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-09-19
    • 2011-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-19
    • 2013-12-22
    相关资源
    最近更新 更多