gpt4 book ai didi

apache-spark - 为什么 createDataFrame 对列重新排序?

转载 作者:行者123 更新时间:2023-12-04 05:26:15 25 4
gpt4 key购买 nike

假设我正在从没有模式的列表中创建一个数据框:

data = [Row(c=0, b=1, a=2), Row(c=10, b=11, a=12)]
df = spark.createDataFrame(data)
df.show()
+---+---+---+
| a| b| c|
+---+---+---+
| 2| 1| 0|
| 12| 11| 10|
+---+---+---+

为什么列按字母顺序重新排序?
我可以在不添加架构的情况下保留列的原始顺序吗?

最佳答案

Why are the columns reordered in alphabet order ?



因为 Row创建于 **kwargs sorts the arguments by name .

这种设计选择是解决 PEP 468 中描述的问题所必需的。 .请查收 SPARK-12467进行讨论。

Can I preserve the original order of columns without adding a schema ?



不与 **kwargs .您可以使用普通 tuples :
df = spark.createDataFrame([(0, 1, 2), (10, 11, 12)], ["c", "b", "a"])

namedtuple :
from collections import namedtuple

CBA = namedtuple("CBA", ["c", "b", "a"])
spark.createDataFrame([CBA(0, 1, 2), CBA(10, 11, 12)])

关于apache-spark - 为什么 createDataFrame 对列重新排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46449404/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com