【发布时间】:2019-03-27 07:55:28
【问题描述】:
给定两个数据帧,除了索引列(本例中为timestamp)之外,它们可能具有完全不同的架构,例如下面的 df1 和 df2:
df1:
timestamp | length | width
1 | 10 | 20
3 | 5 | 3
df2:
timestamp | name | length
0 | "sample" | 3
2 | "test" | 6
如何将这两个数据框组合成一个看起来像这样的数据框:
df3:
timestamp | df1 | df2
| length | width | name | length
0 | null | null | "sample" | 3
1 | 10 | 20 | null | null
2 | null | null | "test" | 6
3 | 5 | 3 | null | null
我对 spark 非常陌生,所以这实际上可能没有多大意义。但我要解决的问题是:我需要组合这些数据框,以便稍后我可以将每一行转换为给定的对象。但是,它们必须按时间戳排序,所以当我写出这些对象时,它们的顺序是正确的。
例如,鉴于上面的df3,我将能够生成以下对象列表:
objs = [
ObjectType1(timestamp=0, name="sample", length=3),
ObjectType2(timestamp=1, length=10, width=20),
ObjectType1(timestamp=2, name="test", length=6),
ObjectType2(timestamp=3, length=5, width=3)
]
也许组合数据帧没有意义,但是我如何单独对数据帧进行排序并以某种方式从每个按timestamp 全局排序的每个数据帧中获取Rows?
P.S.:请注意,我在两个数据帧中都重复了 length。这样做是为了说明它们可能具有相同名称和类型的列,但代表完全不同的数据,因此合并模式是不可能的。
【问题讨论】:
标签: apache-spark pyspark apache-spark-sql