繁体   English   中英

如何过滤 Dataframe 以使 Pyspark 中的某个条件返回 True 或 False?

[英]How to filter a Dataframe in such a way to return a True or False to a certain condition in Pyspark?

我想创建一个返回 False 或 True 到特定条件的数据框,它将替换 pandas 中的内置 function .all 我提供了预期的结果。 提前致谢!

schema = StructType([
StructField( 'vin', StringType(), True),StructField( 'age', IntegerType(), True),StructField( 'var', IntegerType(), True),StructField( 'rim', IntegerType(), True),StructField( 'cap', IntegerType(), True),StructField( 'cur', IntegerType(), True)
  ])

data = [['tom', 10,54,87,23,90], ['nick', 15,63,23,11,65], ['juli', 14,87,9,43,21]]

df=spark.createDataFrame(data,schema)

df.show()
>>>
+----+---+---+---+---+---+
| vin|age|var|rim|cap|cur|
+----+---+---+---+---+---+
| tom| 10| 54| 87| 23| 90|
|nick| 15| 63| 23| 11| 65|
|juli| 14| 87|  9| 43| 21|
+----+---+---+---+---+---+

col_2=['age','var','rim']

df=df.select(*col_2)
df.show()
>>>
+---+---+---+
|age|var|rim|
+---+---+---+
| 10| 54| 87|
| 15| 63| 23|
| 14| 87|  9|
+---+---+---+

df=df.filter(F.col(*col_2) ==10)
#Expected outcome:
>>>
+---===+------+------+
|age   |var   |rim   |
+------+------+------+
| True | False| False|
| False| False| False|
| False| False| False|
+------+------+------+

您可以对每一列和 select 进行比较。 不需要filter

import pyspark.sql.functions as F

df2 = df.select([(F.col(c) == 10).alias(c) for c in col_2])

df2.show()
+-----+-----+-----+
|  age|  var|  rim|
+-----+-----+-----+
| true|false|false|
|false|false|false|
|false|false|false|
+-----+-----+-----+

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM