我正在尝试将 table 的某些列转置为行。我正在使用 pyspark.sql
I'm trying to transpose some columns of my table to row. I'm using pyspark.sql
我正在尝试将 table 的某些列转置为行。我正在使用 pyspark.sql
我的初始数据框:
我想要像这样转置的 Dataframe:
请给我一些解决方案,我该怎么做?
问题解答:
使用简单的 where 子句代替转置。
尝试:
创建一个空数据框 DF2,列为电子邮件、传真、phone
并为每一列使用以下代码片段:
DF2 = DF2.withColumn("email",DF1.select('phone_number').where(DF1['phone_role'] == 'EMAIL'))
DF2 = DF2.withColumn("fax",DF1.select('phone_number').where(DF1['phone_role'] == 'FAX'))
DF2 = DF2.withColumn("phone",DF1.select('phone_number').where(DF1['phone_role'] == 'PHONE'))
稍后您可以删除 DF1 。
我正在尝试将 table 的某些列转置为行。我正在使用 pyspark.sql
我的初始数据框:
我想要像这样转置的 Dataframe:
请给我一些解决方案,我该怎么做?
问题解答:
使用简单的 where 子句代替转置。
尝试:
创建一个空数据框 DF2,列为电子邮件、传真、phone
并为每一列使用以下代码片段:
DF2 = DF2.withColumn("email",DF1.select('phone_number').where(DF1['phone_role'] == 'EMAIL'))
DF2 = DF2.withColumn("fax",DF1.select('phone_number').where(DF1['phone_role'] == 'FAX'))
DF2 = DF2.withColumn("phone",DF1.select('phone_number').where(DF1['phone_role'] == 'PHONE'))
稍后您可以删除 DF1 。