【发布时间】:2021-08-03 20:46:55
【问题描述】:
我有可变数量的列,假设在这个例子中,我们有 4 列要比较 (textX) 与具有不同值的单个列 (id):
d = [
{'id': 500, 'text1': 1000 ,'text2': 2000 ,'text3': 3000, 'text4': 5000},
{'id': 1500, 'text1': 1000 ,'text2': 2000 ,'text3': 3000, 'text4': 5000},
{'id': 2500, 'text1': 1000 ,'text2': 2000 ,'text3': 3000, 'text4': 5000},
{'id': 3500, 'text1': 1000 ,'text2': 2000 ,'text3': 3000, 'text4': 5000},
{'id': 4500, 'text1': 1000 ,'text2': 2000 ,'text3': 3000, 'text4': 5000},
{'id': 5500, 'text1': 1000 ,'text2': 2000 ,'text3': 3000, 'text4': 5000}
]
data = spark.createDataFrame(d)
我想根据“id”的值对textX 列中的最小和更大的值进行操作。
例如,对于id value = 2500,我想对值 2000 和 3000 进行操作。对于值为 500 的“id”,它将是 null 和 1000。
我试图将这些作为附加列,例如获取较低的列值
df_cols = data.columns
thresh_list = [x for x in df_cols if x.startswith('text')]
data.withColumn('inic_th', (col(x) for x in thresh_list if col('id') > col(x)))
但是得到一个错误:
col 应该是 Column
我猜这是因为有多个列符合条件,但无法在此处插入。
有没有人有任何解决方案可以根据第三列将操作转换为 2 个值,或者只是如何正确获得这些界限?实际上,textX 列的数量会有所不同。由于性能问题,我尽可能远离 Pandas 和 UDF。
【问题讨论】:
标签: python apache-spark pyspark apache-spark-sql