python - 将列中的列表拆分为 pyspark 中的一个热编码特征
问题描述
我有一个 pyspark 数据框,如下所示:
ID | tmp_list | 其它功能 |
---|---|---|
1 | ['西班牙','意大利'] | xxx |
2 | ['西班牙','法国','美国','印度'] | 年年 |
3 | ['西班牙','德国'] | zzz |
以及如下国家列表:
EU_countries = ['Spain', 'Italy', 'France', 'Germany']
我想做以下事情:
- 从列中提取所有唯一值
tmp_list
- 为 中存在的所有值创建新列
EU_countries
。对于 中不存在的值EU_countries
,创建一个名为 的列other_countries
。本质上,为列表中的每个条目创建列EU_countries
+ 一个名为other_countries
. - 如果 an
id
包含EU_countries
列表中的任何国家/地区,则新列的值Spain
应1
为 else0
。同样适用于EU_countries
列表中的其他国家。 - 如果 an包含列表
id
中不存在的任何国家/地区,则应填写else列。EU_countries
other_countries
1
0
这是我正在寻找的最终输出:
ID | 西班牙 | 意大利 | 法国 | 德国 | 其他国家 | 其它功能 |
---|---|---|---|---|---|---|
1 | 1 | 1 | 0 | 0 | 0 | xxx |
2 | 1 | 0 | 1 | 0 | 1 | 年年 |
3 | 1 | 0 | 0 | 1 | 0 | zzz |
我为此头破血流。有人可以帮我吗?
任何帮助是极大的赞赏!太感谢了!
解决方案
像我在熊猫中一样推理和工作。
- 爆炸
- 创建将非 EU_countries 归为 other_countries 的类别
- get_dummies。在这一点上,我对这篇文章表示赞赏
代码如下;
df=df.select('*').withColumn('tmp_list1', F.explode(col('tmp_list')))#Create new column with exploded list
df=df.select('*').withColumn('Cat', when(col('tmp_list1').isin(EU_countries),df.tmp_list1).otherwise('other_countries'))#Create another column Cat
df.groupBy("tmp_list",'other features').pivot("Cat").agg(F.lit(1)).na.fill(0).show()#Get dummies
+---------------------------+--------------+------+-------+-----+-----+---------------+
|tmp_list |other features|France|Germany|Italy|Spain|other_countries|
+---------------------------+--------------+------+-------+-----+-----+---------------+
|[Spain, Germany] |zzz |0 |1 |0 |1 |0 |
|[Spain, Italy] |xxx |0 |0 |1 |1 |0 |
|[Spain, France, USA, India]|yyy |1 |0 |0 |1 |1 |
+---------------------------+--------------+------+-------+-----+-----+---------------+
推荐阅读
- c - **(x+i) 和 *(*x+i) 之间的区别
- c# - 无法将 Ioptions 传递给静态扩展方法或预热单例
- python - django 过滤 open_hours close_hours SyntaxError: positional argument follow于关键字参数
- dialogflow-es - Dialogflow 实现未在 Google Assistant 上显示建议
- memory-management - 如何计算 Kubernetes 集群的节点可分配资源?
- python - 有条件地匹配多个单词
- python - 如何读取目录中的所有 HTML 文件并使用 Python 将内容写入 CSV 文件?
- r - 如何在特定字段之前指示值
- python - 有问题将图像文件复制到剪贴板并手动将其粘贴到浏览器中,python
- haskell - 编写应用程序