a.m*_*ssa 76 python apache-spark pyspark spark-dataframe
我处理一个包含两列mvv和count的数据帧.
+---+-----+
|mvv|count|
+---+-----+
| 1 | 5 |
| 2 | 9 |
| 3 | 3 |
| 4 | 1 |
Run Code Online (Sandbox Code Playgroud)
我想获得两个包含mvv值和计数值的列表.就像是
mvv = [1,2,3,4]
count = [5,9,3,1]
Run Code Online (Sandbox Code Playgroud)
所以,我尝试了以下代码:第一行应该返回一个python列表行.我想看到第一个值:
mvv_list = mvv_count_df.select('mvv').collect()
firstvalue = mvv_list[0].getInt(0)
Run Code Online (Sandbox Code Playgroud)
但是我收到第二行的错误消息:
AttributeError:getInt
Thi*_*dim 109
看,为什么你这样做不起作用.首先,您尝试从行类型中获取整数,您的collect的输出如下所示:
>>> mvv_list = mvv_count_df.select('mvv').collect()
>>> mvv_list[0]
Out: Row(mvv=1)
Run Code Online (Sandbox Code Playgroud)
如果你采取这样的事情:
>>> firstvalue = mvv_list[0].mvv
Out: 1
Run Code Online (Sandbox Code Playgroud)
你会得到mvv
价值.如果你想要数组的所有信息,你可以采取这样的方式:
>>> mvv_array = [int(row.mvv) for row in mvv_list.collect()]
>>> mvv_array
Out: [1,2,3,4]
Run Code Online (Sandbox Code Playgroud)
但是,如果您对其他列尝试相同的操作,则会得到:
>>> 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'
Run Code Online (Sandbox Code Playgroud)
发生这种情况是因为它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()]
Run Code Online (Sandbox Code Playgroud)
但是不需要此解决方法,因为您可以使用字典语法访问该列:
>>> mvv_array = [int(row['mvv']) for row in mvv_list.collect()]
>>> mvv_count = [int(row['count']) for row in mvv_list.collect()]
Run Code Online (Sandbox Code Playgroud)
它终将奏效!
Neo*_*Neo 79
在一个班轮后面给出你想要的清单.
mvv = mvv_count_df.select("mvv").rdd.flatMap(lambda x: x).collect()
Run Code Online (Sandbox Code Playgroud)
Pow*_*ers 19
我进行了基准分析,这list(mvv_count_df.select('mvv').toPandas()['mvv'])
是最快的方法。我很惊讶。
我使用 Spark 2.4.5 的 5 节点 i3.xlarge 集群(每个节点有 30.5 GB 的 RAM 和 4 个内核)在 10 万/1 亿行数据集上运行了不同的方法。数据均匀分布在 20 个压缩的 Parquet 文件中,只有一列。
这是基准测试结果(以秒为单位的运行时间):
+-------------------------------------------------------------+---------+-------------+
| Code | 100,000 | 100,000,000 |
+-------------------------------------------------------------+---------+-------------+
| df.select("col_name").rdd.flatMap(lambda x: x).collect() | 0.4 | 55.3 |
| list(df.select('col_name').toPandas()['col_name']) | 0.4 | 17.5 |
| df.select('col_name').rdd.map(lambda row : row[0]).collect()| 0.9 | 69 |
| [row[0] for row in df.select('col_name').collect()] | 1.0 | OOM |
| [r[0] for r in mid_df.select('col_name').toLocalIterator()] | 1.2 | * |
+-------------------------------------------------------------+---------+-------------+
* cancelled after 800 seconds
Run Code Online (Sandbox Code Playgroud)
在驱动程序节点上收集数据时要遵循的黄金规则:
toPandas
在 Spark 2.3 中得到了显着改进。如果您使用的是早于 2.3 的 Spark 版本,这可能不是最好的方法。
有关更多详细信息/基准测试结果,请参见此处。
Muh*_*min 16
这将为您提供列表中的所有元素.
mvv_list = list(
mvv_count_df.select('mvv').toPandas()['mvv']
)
Run Code Online (Sandbox Code Playgroud)
Ita*_*chi 11
以下代码将为您提供帮助
mvv_count_df.select('mvv').rdd.map(lambda row : row[0]).collect()
Run Code Online (Sandbox Code Playgroud)
小智 8
一个可能的解决方案是使用collect_list()
中的函数pyspark.sql.functions
。这会将所有列值聚合到一个 pyspark 数组中,该数组在收集时会转换为 python 列表:
mvv_list = df.select(collect_list("mvv")).collect()[0][0]
count_list = df.select(collect_list("count")).collect()[0][0]
Run Code Online (Sandbox Code Playgroud)
您可以首先收集 df 并将返回 Row 类型的列表
row_list = df.select('mvv').collect()
Run Code Online (Sandbox Code Playgroud)
迭代行以转换为列表
sno_id_array = [ int(row.mvv) for row in row_list]
sno_id_array
[1,2,3,4]
Run Code Online (Sandbox Code Playgroud)
使用平面图
sno_id_array = df.select("mvv").rdd.flatMap(lambda x: x).collect()
Run Code Online (Sandbox Code Playgroud)
小智 6
如果您收到以下错误:
AttributeError: 'list' 对象没有属性 'collect'
此代码将解决您的问题:
mvv_list = mvv_count_df.select('mvv').collect()
mvv_array = [int(i.mvv) for i in mvv_list]
Run Code Online (Sandbox Code Playgroud)
根据我的数据,我得到了这些基准:
>>> data.select(col).rdd.flatMap(lambda x: x).collect()
Run Code Online (Sandbox Code Playgroud)
0.52秒
>>> [row[col] for row in data.collect()]
Run Code Online (Sandbox Code Playgroud)
0.271秒
>>> list(data.select(col).toPandas()[col])
Run Code Online (Sandbox Code Playgroud)
0.427秒
结果是一样的
让我们创建有问题的数据框
df_test = spark.createDataFrame(
[
(1, 5),
(2, 9),
(3, 3),
(4, 1),
],
['mvv', 'count']
)
df_test.show()
Run Code Online (Sandbox Code Playgroud)
这使
+---+-----+
|mvv|count|
+---+-----+
| 1| 5|
| 2| 9|
| 3| 3|
| 4| 1|
+---+-----+
Run Code Online (Sandbox Code Playgroud)
然后应用 rdd.flatMap(f).collect() 来获取列表
test_list = df_test.select("mvv").rdd.flatMap(list).collect()
print(type(test_list))
print(test_list)
Run Code Online (Sandbox Code Playgroud)
这使
<type 'list'>
[1, 2, 3, 4]
Run Code Online (Sandbox Code Playgroud)
小智 5
when
尽管有很多答案,但当您需要将列表与和isin
命令结合使用时,其中一些答案将不起作用。产生扁平值列表的最简单而有效的方法是使用列表理解并[0]
避免行名称:
flatten_list_from_spark_df=[i[0] for i in df.select("your column").collect()]
Run Code Online (Sandbox Code Playgroud)
另一种方法是使用 panda 数据框,然后使用该list
功能,但它不如 this 方便和有效。
归档时间: |
|
查看次数: |
122285 次 |
最近记录: |