python-3.x - 在pyspark中将值随机更改为空值的最有效方法是什么?
问题描述
试图弄清楚如何用空值随机替换 Pyspark 中的特定列。所以改变这样的数据框:
| A | B |
|----|----|
| 1 | 2 |
| 3 | 4 |
| 5 | 6 |
| 7 | 8 |
| 9 | 10 |
| 11 | 12 |
并将“B”列中 25% 的值随机更改为空值:
| A | B |
|----|------|
| 1 | 2 |
| 3 | NULL |
| 5 | 6 |
| 7 | NULL |
| 9 | NULL |
| 11 | 12 |
解决方案
thanks to @pault I was able to answer my own question using the question he posted that you can find here
Essentially I ran something like this:
import pyspark.sql.functions as f
df1 = df.withColumn('Val', f.when(f.rand() > 0.25, df1['Val']).otherwise(f.lit(None))
Which will randomly select values with the column 'Val' and make it into a None value
推荐阅读
- python - Django 'ModelForm' 对象没有属性 'object'
- fortran - Fortran如何编写非零元素
- javascript - 如何在 vue.js 中循环一组复选框并使每组相互独立
- javascript - AngularJS Ajax 内容加载器
- r - 如何让自定义函数在被中断时返回值?
- java - 无法将类型 id 解析为简单类型的子类型 - Spring Boot 中的内部枚举
- angularjs - UI 路由器解决不适用于基于组件的状态
- azure - 为什么什么都上传不了?- 使用 Xamarin 将信息 blob 上传到 Azure
- json - Google Cloud Vision API : "error code: 3" , "message": "Bad image data."
- python - 解密 .py 代码