将宽数据帧转换为长数据帧 [英] Transpose wide dataframe to long dataframe

查看:77
本文介绍了将宽数据帧转换为长数据帧的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据框,看起来像:

I have a data frame looks like:

Region, 2000Q1, 2000Q2, 2000Q3, ...
A, 1,2,3,...

我想通过"Region"将此宽表转换为长表.因此最终产品将如下所示:

I want to transpose this wide table to a long table by 'Region'. So the final product will look like:

Region, Time, Value
A, 2000Q1,1
A, 2000Q2, 2
A, 2000Q3, 3
A, 2000Q4, 4
....

原始表的列非常广泛,但是聚合级别始终是区域,其余列设置为可转置.

The original table has a very wide array of columns but the aggregation level is always region and remaining columns are set to be tranposed.

您知道执行此操作的简单方法或功能吗?

Do you know an easy way or function to do this?

推荐答案

类似,但针对列计算进行了改进.

Similar but improved for the column calculation.

cols = df.columns
cols.remove('Region')

import pyspark.sql.functions as f

df.withColumn('array', f.explode(f.arrays_zip(f.array(*map(lambda x: f.lit(x), cols)), f.array(*cols), ))) \
  .select('Region', 'array.*') \
  .toDF('Region', 'Time', 'Value') \
  .show(30, False)

+------+------+-----+
|Region|Time  |Value|
+------+------+-----+
|A     |2000Q1|1    |
|A     |2000Q2|2    |
|A     |2000Q3|3    |
|A     |2000Q4|4    |
|A     |2000Q5|5    |
+------+------+-----+

p.s.不要接受这个作为答案:)

p.s. Don't accept this as an answer :)

这篇关于将宽数据帧转换为长数据帧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆