【发布时间】:2017-07-20 22:16:18
【问题描述】:
我有一个现有的 pyspark 数据框,它有 170 列和 841 行。我希望在其中添加另一列,即“字符串”列表。列表长度为 841,名称为 totals
>>> totals
['165024392279', '672183', '1002643', '202292', '216254163906', '4698279464', '9247442818', '60093051178', '22208366804', '994475', '12174', '9404969384', '32118344368', '857443', '48544', '24572495416', '43802661492', '35686122552', '780813', '35414800642', '661474', '531615', '31962803064', '111295163538', '531671', '25776968294', '78538019255', '152455113964', '39305504103', '325507', '1028244', '82294034461', '715748', '12705147430', '678604', '90303771130', '1372443', '362131', '59079186929', '436218', '79528', '41366', '89254591311'...]
其中一种方法是创建一个新的数据框并将其与主数据框连接。
new_df = sqlContext.createDataFrame([Row(**{'3G-fixated voice users':t})for t in totals])
所以 new_df 有 1 列 841 行。并且它不能连接到原始数据框,因为没有可连接的公共列。
我能想到的另一种半生不熟的方法是使用文字。
from pyspark.sql.functions import array,lit
totals=[str(t) for t in totals]
test_lit = array([array([lit(t) for t in tt]) for tt in totals])
big_df.withColumn('3G-fixated voice users',test_lit)
这会添加一个类型为
的新列array<array<string>>
所有的值都只在第一行,这是不需要的。
当列表的长度与数据框中的行数相同时,有没有办法从列表中添加新列?
仍然是使用 pyspark 的新手
【问题讨论】:
标签: python list dataframe pyspark