如何将数据框的所有列转换为字符串 [英] how to cast all columns of dataframe to string
本文介绍了如何将数据框的所有列转换为字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个混合类型的数据框.我正在使用从 hive 表中读取此数据框spark.sql('select a,b,c from table')
命令.
I have a mixed type dataframe.
I am reading this dataframe from hive table using
spark.sql('select a,b,c from table')
command.
某些列是 int 、 bigint 、 double ,而其他列是字符串.共有 32 列.pyspark 中是否有任何方法可以将数据框中的所有列转换为字符串类型?
Some columns are int , bigint , double and others are string. There are 32 columns in total. Is there any way in pyspark to convert all columns in the data frame to string type ?
推荐答案
Just:
from pyspark.sql.functions import col
table = spark.sql("table")
table.select([col(c).cast("string") for c in table.columns])
这篇关于如何将数据框的所有列转换为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文