apache-spark - 在数据框 pyspark 的多列上应用函数
问题描述
我有一个包含 30 列的数据框。但是我想专门在 3 列上应用一个函数。我做了这样的事情:
# these are the 3 columns of a dataframe df and they are of StringType
date_columns = ['date1', 'date2', 'date3']
# my function
def convert_date(x):
if x is '':
return ''
else:
return f.expr("date_add(to_date('1899-12-30'), cast(x as int))").cast(StringType())
# Want to do something like this
for c in date_columns:
df = df.withColumn(c, convert_date(df[c]))
这是实现这一目标的正确方法吗?也许我在这里遗漏了一些东西。
解决方案
推荐阅读
- c# - 基于用户 ID 的灵活命名分区的领域同步权限
- python - 从python中的函数中打印函数的id
- android - 获取当前在 GoogleMap 上可见的 POI(兴趣点)列表?
- python-3.x - 删除数组结果中不必要的信息
- facebook - 关闭并重新打开互联网时,使用 facebook 和 google 登录的 Firebase 身份验证不起作用
- c++ - ubuntu 20.04 上的 c++17 并行算法和 CMake
- php - Yii2 memcached 不工作但 php 简单连接工作
- excel - VBA在5个整数列表中获取前2个整数值
- xcode - 如何永久更改 xcode 默认的“构建设置”
- sql - 当我尝试在 sql 的视图中更改 max(value) 的列名时,它显示错误