pyspark中的toPandas
方法对于数值列中的空值不一致。有没有办法迫使它更加一致
示例
sc
是sparkContext。spark版本是2.3.2。我不确定如何包括笔记本的结果,但我将只评论输出。这很简单,你可以自己在笔记本上查看
sparkTest = sc.createDataFrame(
[
(1, 1 ),
(2, None),
(None, None),
],
['a', 'b']
)
sparkTest.show() # all None values are neatly converted to null
pdTest1 = sparkTest.toPandas()
pdTest1 # all None values are NaN
np.isnan(pdTest1['b']) # this a series of dtype bool
pdTest2 = sparkTest.filter(col('b').isNull()).toPandas()
pdTest2 # the null value in column a is still NaN, but the two null in column b are now None
np.isnan(pdTest2['b']) # this throws an error
在编程时,这当然是有问题的,而且无法预先预测列是否全部为空
顺便说一句,我想把这当作一个问题来报告,但我不知道在哪里。{a1}似乎没有问题部分
np.isnan
可以应用于本机数据类型的NumPy数组(如np.float64),但应用于对象数组时会引发TypeError
:如果您有熊猫,您可以使用
pandas.isnull
:这对于
np.nan
和None
都是一致的或者,您可以(如果可能的话)将
pdTest2['b']
数组强制转换为本机numpy
类型之一(例如np.float64
),以确保np.isnan
正常工作,例如:相关问题 更多 >
编程相关推荐