将 spark DataFrame 列转换为 python 列表 [英] Convert spark DataFrame column to python list

查看:35
本文介绍了将 spark DataFrame 列转换为 python 列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我处理一个包含两列、mvv 和 count 的数据框.

+---+-----+|mvv|计数|+---+-----+|1 |5 ||2 |9 ||3 |3 ||4 |1 |

我想获得两个包含 mvv 值和计数值的列表.类似的东西

mvv = [1,2,3,4]计数 = [5,9,3,1]

所以,我尝试了以下代码:第一行应该返回一个 Python 行列表.我想看到第一个值:

mvv_list = mvv_count_df.select('mvv').collect()firstvalue = mvv_list[0].getInt(0)

但我在第二行收到一条错误消息:

<块引用>

属性错误:getInt

解决方案

看,为什么你的这种方式行不通.首先,您试图从 Row 类型,你collect的输出是这样的:

<预><代码>>>>mvv_list = mvv_count_df.select('mvv').collect()>>>mvv_list[0]出:行(mvv=1)

如果你采取这样的做法:

<预><代码>>>>firstvalue = mvv_list[0].mvv出:1

您将获得 mvv 值.如果你想要数组的所有信息,你可以这样:

<预><代码>>>>mvv_array = [int(row.mvv) for row in mvv_list.collect()]>>>mvv_array输出:[1,2,3,4]

但是如果你对另一列尝试同样的方法,你会得到:

<预><代码>>>>mvv_count = [int(row.count) for row in mvv_list.collect()]输出:类型错误:int() 参数必须是字符串或数字,而不是 'builtin_function_or_method'

发生这种情况是因为 count 是一个内置方法.并且该列与 count 具有相同的名称.一种解决方法是将 count 的列名更改为 _count:

<预><代码>>>>mvv_list = mvv_list.selectExpr("mvv as mvv", "count as _count")>>>mvv_count = [int(row._count) for row in mvv_list.collect()]

但是不需要这种解决方法,因为您可以使用字典语法访问该列:

<预><代码>>>>mvv_array = [int(row['mvv']) for row in mvv_list.collect()]>>>mvv_count = [int(row['count']) for row in mvv_list.collect()]

它最终会起作用!

I work on a dataframe with two column, mvv and count.

+---+-----+
|mvv|count|
+---+-----+
| 1 |  5  |
| 2 |  9  |
| 3 |  3  |
| 4 |  1  |

i would like to obtain two list containing mvv values and count value. Something like

mvv = [1,2,3,4]
count = [5,9,3,1]

So, I tried the following code: The first line should return a python list of row. I wanted to see the first value:

mvv_list = mvv_count_df.select('mvv').collect()
firstvalue = mvv_list[0].getInt(0)

But I get an error message with the second line:

AttributeError: getInt

解决方案

See, why this way that you are doing is not working. First, you are trying to get integer from a Row Type, the output of your collect is like this:

>>> mvv_list = mvv_count_df.select('mvv').collect()
>>> mvv_list[0]
Out: Row(mvv=1)

If you take something like this:

>>> firstvalue = mvv_list[0].mvv
Out: 1

You will get the mvv value. If you want all the information of the array you can take something like this:

>>> mvv_array = [int(row.mvv) for row in mvv_list.collect()]
>>> mvv_array
Out: [1,2,3,4]

But if you try the same for the other column, you get:

>>> mvv_count = [int(row.count) for row in mvv_list.collect()]
Out: TypeError: int() argument must be a string or a number, not 'builtin_function_or_method'

This happens because count is a built-in method. And the column has the same name as count. A workaround to do this is change the column name of count to _count:

>>> mvv_list = mvv_list.selectExpr("mvv as mvv", "count as _count")
>>> mvv_count = [int(row._count) for row in mvv_list.collect()]

But this workaround is not needed, as you can access the column using the dictionary syntax:

>>> mvv_array = [int(row['mvv']) for row in mvv_list.collect()]
>>> mvv_count = [int(row['count']) for row in mvv_list.collect()]

And it will finally work!

这篇关于将 spark DataFrame 列转换为 python 列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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