PySpark:如何为特定列填充数据框中的值? [英] PySpark: How to fillna values in dataframe for specific columns?
本文介绍了PySpark:如何为特定列填充数据框中的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下示例DataFrame:
I have the following sample DataFrame:
a | b | c |
1 | 2 | 4 |
0 | null | null|
null | 3 | 4 |
我只想在前两列中替换空值-列"a"和"b":
And I want to replace null values only in the first 2 columns - Column "a" and "b":
a | b | c |
1 | 2 | 4 |
0 | 0 | null|
0 | 3 | 4 |
以下是创建示例数据框的代码:
Here is the code to create sample dataframe:
rdd = sc.parallelize([(1,2,4), (0,None,None), (None,3,4)])
df2 = sqlContext.createDataFrame(rdd, ["a", "b", "c"])
我知道如何使用以下方法替换所有空值:
I know how to replace all null values using:
df2 = df2.fillna(0)
当我尝试此操作时,我输了第三列:
And when I try this, I lose the third column:
df2 = df2.select(df2.columns[0:1]).fillna(0)
推荐答案
df.fillna(0, subset=['a', 'b'])
有一个名为subset
的参数可以选择列,除非您的Spark版本低于1.3.1
There is a parameter named subset
to choose the columns unless your spark version is lower than 1.3.1
这篇关于PySpark:如何为特定列填充数据框中的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文