【发布时间】:2020-04-21 08:55:59
【问题描述】:
我想在我的 Spark DataFrame 上动态应用 .withColumn,列名在 list
from pyspark.sql.functions import col
from pyspark.sql.types import BooleanType
def get_dtype(dataframe,colname):
return [dtype for name, dtype in dataframe.dtypes if name == colname][0]
def get_matches(dataframe):
return [x for x in dataframe.columns if get_dtype(dataframe,x)=='tinyint']
matches = get_matches(srcpartyaddressDF)
matches
上面的代码给了我列数据类型为'tinyint'的列列表
结果:
Out[67]: ['verified_flag', 'standard_flag', 'overseas_flag', 'active']
现在我想对列表 matches 中的每一列动态地执行以下操作
partyaddressDF = srcpartyaddressDF.withColumn("verified_flag", col("verified_flag").cast(BooleanType())).withColumn("standard_flag", col("standard_flag").cast(BooleanType())).withColumn("overseas_flag", col("overseas_flag").cast(BooleanType())).withColumn("active", col("active").cast(BooleanType()))
如何在 Python3 中实现这一点
【问题讨论】:
标签: python-3.x apache-spark-sql azure-databricks