kww*_*kww 7 python apache-spark apache-spark-sql pyspark pyspark-sql
假设我们有一个名为df
. 我知道有一种使用df.dtypes
. 但是我更喜欢类似的东西
type(123) == int # note here the int is not a string
我想知道是否有类似的东西:
type(df.select(<column_name>).collect()[0][1]) == IntegerType
基本上我想知道如何直接IntegerType, StringType
从dataframe中获取类的对象然后进行判断。
谢谢!
hi-*_*zir 10
TL;DR使用外部数据类型(纯 Python 类型)来测试值,使用内部数据类型(DataType
子类)来测试模式。
首先也是最重要的 - 你永远不应该使用
type(123) == int
Run Code Online (Sandbox Code Playgroud)
在处理继承的 Python 中检查类型的正确方法是
isinstance(123, int)
Run Code Online (Sandbox Code Playgroud)
做完这些,让我们谈谈
基本上我想知道如何直接从dataframe中获取IntegerType,StringType之类的类的对象,然后进行判断。
这不是它的工作原理。DataTypes
描述模式(内部表示)而不是值。根据Spark SQL Programming guide 中定义的规则,外部类型是一个普通的 Python 对象,所以如果内部类型是IntegerType
,那么外部类型是int
等等。
IntegerType
(或其他DataTypes
)实例存在的唯一地方是您的架构:
from pyspark.sql.types import *
df = spark.createDataFrame([(1, "foo")])
isinstance(df.schema["_1"].dataType, LongType)
# True
isinstance(df.schema["_2"].dataType, StringType)
# True
_1, _2 = df.first()
isinstance(_1, int)
# True
isinstance(_2, str)
# True
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
6977 次 |
最近记录: |