我有一个类似于下面的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|
+------+------+-----+
最简单的方法是什么?
4条答案
按热度按时间huwehgph1#
when+otherwise
我要做的是:iih3973s2#
另一种不那么冗长的方法是使用
replace
.esbemjvw3#
使用
reduce
申请when+otherwise
在dataframe的所有列上。ct3nt3jp4#
可以扫描所有列并替换
-1
没有的:输出: