将列表作为列添加到 pyspark 中的 Dataframe [英] Add list as column to Dataframe in pyspark

查看:77
本文介绍了将列表作为列添加到 pyspark 中的 Dataframe的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个整数列表和一个 sqlcontext 数据框,其行数等于列表的长度.我想将列表作为一列添加到此数据框中,以保持顺序.我觉得这应该很简单,但我找不到优雅的解决方案.

I have a list of integers and a sqlcontext dataframe with the number of rows equal to the length of the list. I want to add the list as a column to this dataframe maintaining the order. I feel like this should be really simple but I can't find an elegant solution.

推荐答案

您不能简单地将列表添加为数据框列,因为列表是本地对象而数据框是分布式的.您可以尝试以下方法之一:

You cannot simply add a list as a dataframe column since list is local object and dataframe is distirbuted. You can try one of thw followin approaches:

  • 通过 collect()toLocalIterator() 将数据帧转换为本地,并为每一行添加列表中的相应值 OR
  • 将列表转换为数据框添加一个额外的列(使用来自数据框的键),然后将它们连接起来
  • convert dataframe to local by collect() or toLocalIterator() and for each row add corresponding value from the list OR
  • convert list to dataframe adding an extra column (with keys from dataframe) and then join them both

这篇关于将列表作为列添加到 pyspark 中的 Dataframe的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆