PySpark:如何为所有列强制转换字符串数据类型

西塔拉姆奇克拉

我的主要目标是将任何df的所有列都转换为字符串,这样比较就容易了。

我已经在下面尝试了多种建议的方法。但不能成功:

target_df = target_df.select([col(c).cast("string") for c in target_df.columns])

这给了错误:

pyspark.sql.utils.AnalysisException: "Can't extract value from SDV#155: need struct type but got string;"

我尝试的下一个是:

target_df = target_df.select([col(c).cast(StringType()).alias(c) for c in columns_list])

错误:

pyspark.sql.utils.AnalysisException: "Can't extract value from SDV#27: need struct type but got string;"

下一个方法是:

        for column in target_df.columns:
             target_df = target_df.withColumn(column, target_df[column].cast('string'))

错误:

pyspark.sql.utils.AnalysisException: "Can't extract value from SDV#27: need struct type but got string;"

在强制转换之前存在的几行代码:

        columns_list = source_df.columns.copy()
        target_df = target_df.toDF(*columns_list)

我正在尝试的示例df模式:

root
 |-- A: string (nullable = true)
 |-- S: string (nullable = true)
 |-- D: string (nullable = true)
 |-- F: string (nullable = true)
 |-- G: double (nullable = true)
 |-- H: double (nullable = true)
 |-- J: string (nullable = true)
 |-- K: string (nullable = true)
 |-- L: string (nullable = true)
 |-- M: string (nullable = true)
 |-- N: string (nullable = true)
 |-- B: string (nullable = true)
 |-- V: string (nullable = true)
 |-- C: string (nullable = true)
 |-- X: string (nullable = true)
 |-- Y: string (nullable = true)
 |-- U: double (nullable = true)
 |-- I: string (nullable = true)
 |-- R: string (nullable = true)
 |-- T: string (nullable = true)
 |-- Q: string (nullable = true)
 |-- E: double (nullable = true)
 |-- W: string (nullable = true)
 |-- AS: string (nullable = true)
 |-- DSC: string (nullable = true)
 |-- DCV: string (nullable = true)
 |-- WV: string (nullable = true)
 |-- SDV: string (nullable = true)
 |-- SDV.1: string (nullable = true)
 |-- WDV: string (nullable = true)
 |-- FWFV: string (nullable = true)
 |-- ERBVSER: string (nullable = true)
x

如建议的那样,错误是.由于列中的点引起的SDV.1选择该列时必须用反引号将其括起来:

for column in target_df.columns:
    target_df = target_df.withColumn(column, target_df['`{}`'.format(column)].cast('string'))

要么

target_df = target_df.select([col('`{}`'.format(c)).cast(StringType()).alias(c) for c in columns_list])

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如果所有Java字符串都是UTF-16字符串,那么char数据类型的最大大小如何为2?

将混合数据类型的元组列表转换为所有字符串

如何将json中的数据类型从数字转换为字符串,同时保持所有对象值原样打字稿

如何使用python在SQL Server中将字符串数据类型类型的行转换为XML数据类型

Python中是否有无法以字符串类型转换的数据类型

如何将所有双精度和字符串数据类型分离到实例的集合中

如何将数据帧数据类型转换为字符串?

从字符串到TimeStamp数据类型的转换

将数据类型转换为查询字符串

PHP sqlite返回所有字符串,无论定义了哪种数据类型

转换具有单个混合数据类型数组的单个字符串

如何在 Haskell 中将字符串转换为枚举数据类型?

如何解决Pojo中字符串/日期字段数据类型转换的问题?

如何在熊猫中将列的数据类型从字符串转换为列表?

如何将字符串转换为自定义数据类型

如何在配置单元中将字符串数据类型列转换为日期格式

如何将“frozenset({})”字符串转换为数据类型frozenset?

将字符串引用数据类型转换为真正的引用数据类型

熊猫:将列强制转换为字符串不起作用

雄辩的数据表将所有属性强制转换为字符串

如何将所有列数据类型动态转换为数字和字符?

如何在PySpark数据帧上的列(具有(字符串的)数据类型数组)上应用过滤器?

具有强制数据类型和维数的输入numpy数组的文档字符串格式

删除pandas数据框中具有混合数据类型的所有行,这些数据类型包含多列中的特定字符串

将列表的列强制为R数据帧中的字符串

如何确定作为字符串传递的数值的类型而不丢失数据或转换为错误的数据类型

将所有数据框列强制转换为浮点的最快方法-Pandas Astype Slow

如何从字符串中检测数据类型?

仅在适用的情况下如何从字符串列强制转换和更新数值