pyspark:在dataframe中用null替换所有出现的值

roqulrg3  于 2021-05-27  发布在  Spark
关注(0)|答案(4)|浏览(1007)

我有一个类似于下面的Dataframe。我最初用-1填充所有空值,以便在pyspark中进行连接。

df = pd.DataFrame({'Number': ['1', '2', '-1', '-1'],
                   'Letter': ['A', '-1', 'B', 'A'],
                   'Value': [30, 30, 30, -1]})

pyspark_df = spark.createDataFrame(df)

+------+------+-----+
|Number|Letter|Value|
+------+------+-----+
|     1|     A|   30|
|     2|    -1|   30|
|    -1|     B|   30|
|    -1|     A|   -1|
+------+------+-----+

处理完数据集后,我需要将所有-1值替换回空值。

+------+------+-----+
|Number|Letter|Value|
+------+------+-----+
|     1|     A|   30|
|     2|  null|   30|
|  null|     B|   30|
|  null|     A| null|
+------+------+-----+

最简单的方法是什么?

huwehgph

huwehgph1#

when+otherwise 我要做的是:

import pyspark.sql.functions as F

pyspark_df.select([F.when(F.col(i).cast("Integer") <0 , None).otherwise(F.col(i)).alias(i)
                  for i in df.columns]).show()
+------+------+-----+
|Number|Letter|Value|
+------+------+-----+
|     1|     A|   30|
|     2|  null|   30|
|  null|     B|   30|
|  null|     A| null|
+------+------+-----+
iih3973s

iih3973s2#

另一种不那么冗长的方法是使用 replace .

pyspark_df.replace(-1,None).replace('-1',None).show()
esbemjvw

esbemjvw3#

使用 reduce 申请 when+otherwise 在dataframe的所有列上。

df.show()

# +------+------+-----+

# |Number|Letter|Value|

# +------+------+-----+

# |     1|     A|   30|

# |     2|    -1|   30|

# |    -1|     B|   30|

# +------+------+-----+

from functools import reduce

(reduce(lambda new_df, col_name: new_df.withColumn(col_name, when(col(col_name)== '-1',lit(None)).otherwise(col(col_name))),df.columns,df)).show()

# +------+------+-----+

# |Number|Letter|Value|

# +------+------+-----+

# |     1|     A|   30|

# |     2|  null|   30|

# |  null|     B|   30|

# +------+------+-----+
ct3nt3jp

ct3nt3jp4#

可以扫描所有列并替换 -1 没有的:

import pyspark.sql.functions as F

for x in pyspark_df.columns:
    pyspark_df = pyspark_df.withColumn(x, F.when(F.col(x)==-1, F.lit(None)).otherwise(F.col(x)))

pyspark_df.show()

输出:

+------+------+-----+
|Number|Letter|Value|
+------+------+-----+
|     1|     A|   30|
|     2|  null|   30|
|  null|     B|   30|
|  null|     A| null|
+------+------+-----+

相关问题