【问题标题】:Multiple conditons withColumn perfomance recomendations具有色谱柱性能建议的多种条件
【发布时间】:2021-02-05 21:19:10
【问题描述】:

这是我的代码。我得到了正确的输出,但我需要很长时间,而且我觉得我重复自己太多次了。

我的问题是有另一种方法可以检查数据库行的条件吗?

提前感谢您的建议

Output = (db.withColumn('Puhelin', F.when(F.col('source_browser_platform')=='Android OS' ,F.lit('1')).otherwise(F.lit('0')))
     .withColumn('Puhelin', F.when(F.col('source_browser_platform')=='iOS',F.lit('1')).otherwise(F.lit('0')))
     .withColumn('Puhelin', F.when(F.col('source_browser_platform')=='OS/2',F.lit('1')).otherwise(F.lit('0')))
     .withColumn('Puhelin', F.when(F.col('source_browser_platform')=='Windows Mobile',F.lit('1')).otherwise(F.lit('0')))
     .withColumn('Puhelin', F.when(F.col('source_browser_platform')=='Chrome OS',F.lit('1')).otherwise(F.lit('0')))
     .withColumn('Puhelin', F.when(F.col('source_browser_platform')=='BlackBerry OS',F.lit('1')).otherwise(F.lit('0')))
     .withColumn('Puhelin', F.when(F.col('source_browser_platform')=='Windows XP',F.lit('1')).otherwise(F.lit('0')))
     .withColumn('Tietokone', F.when(F.col('source_browser_platform')=='Windows 10',F.lit('1')).otherwise(F.lit('0')))
     .withColumn('Tietokone', F.when(F.col('source_browser_platform')=='Windows 8',F.lit('1')).otherwise(F.lit('0')))
     .withColumn('Tietokone', F.when(F.col('source_browser_platform')=='Windows 8.1',F.lit('1')).otherwise(F.lit('0')))
     .withColumn('Tietokone', F.when(F.col('source_browser_platform')=='Mac OS',F.lit('1')).otherwise(F.lit('0')))
     .withColumn('Tietokone', F.when(F.col('source_browser_platform')=='Linux',F.lit('1')).otherwise(F.lit('0')))
     .withColumn('Tietokone', F.when(F.col('source_browser_platform')=='Windows 2000',F.lit('1')).otherwise(F.lit('0')))
     .withColumn('Tietokone', F.when(F.col('source_browser_platform')=='Windows Server 2003',F.lit('1')).otherwise(F.lit('0')))
     .withColumn('Tietokone', F.when(F.col('source_browser_platform')=='Sun OS',F.lit('1')).otherwise(F.lit('0')))
     .withColumn('Tietokone', F.when(F.col('source_browser_platform')=='Windows Vista',F.lit('1')).otherwise(F.lit('0')))
     .withColumn('Tietokone', F.when(F.col('source_browser_platform')=='Open BSD',F.lit('1')).otherwise(F.lit('0')))
     .withColumn('Tietokone', F.when(F.col('source_browser_platform')=='Windows 7',F.lit('1')).otherwise(F.lit('0')))
    )

【问题讨论】:

  • 您似乎在尝试检查平台是移动平台还是桌面平台,但是在您编写它的方式上,它只会检查它是 windows xp 还是 windows 7,因为您是覆盖行

标签: apache-spark pyspark apache-spark-sql


【解决方案1】:

您可以使用isin 来检查列是否在给定列表中:

puhelin_list = ['Android OS', 'iOS', ...]
tietokone_list = ['Windows 10', 'Windows 8', ...]

import pyspark.sql.functions as F

output = db.withColumn(
    'puhelin', F.col('source_browser_platform').isin(puhelin_list).cast('int')
).withColumn(
    'Tietokone', F.col('source_browser_platform').isin(tietokone_list).cast('int')
)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-19
    • 2019-05-08
    • 1970-01-01
    • 2015-01-23
    相关资源
    最近更新 更多