【发布时间】: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