【发布时间】:2017-08-31 15:33:12
【问题描述】:
我看到了this post,它有点帮助,只是我需要使用列表更改数据帧的标题,因为它很长并且随着我输入的每个数据集而变化,所以我无法真正写出/很难-新列名中的代码。
例如:
df = sqlContext.read.load("./assets/"+filename,
format='com.databricks.spark.csv',
header='false',
inferSchema='false')
devices = df.first()
metrics = df.take(2)[1]
# Adding the two header rows together as one as a way of later searching through and sorting rows
# delimiter is "..." since it doesn't occur anywhere in the data and we don't have to wory about multiple splits
header = [str(devices[i]) +"..."+ str(metrics[i]) for i in range(len(devices))]
df2 = df.toDF(header)
那么我当然会得到这个错误:
IllegalArgumentException: u"requirement failed: 列数不匹配。\n旧列名 (278):
header 的长度 = 278 和列数是一样的。 所以,真正的问题是,当我有一个新名称列表时,如何对数据帧中的标头进行非硬编码重命名?
我怀疑我必须不以实际列表对象的形式输入,但是如何在不遍历每一列的情况下做到这一点(使用 selectexpr 或别名并使用一个创建几个新的 dfs(不可变)一次更新新的列?(糟糕)
【问题讨论】:
-
len(devices)会返回什么?
标签: python apache-spark pyspark spark-dataframe