Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/363.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/apache-spark/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Pyspark将标准列表转换为数据帧_Python_Apache Spark_Pyspark_Pyspark Sql - Fatal编程技术网

Python Pyspark将标准列表转换为数据帧

Python Pyspark将标准列表转换为数据帧,python,apache-spark,pyspark,pyspark-sql,Python,Apache Spark,Pyspark,Pyspark Sql,这个案例非常简单,我需要使用以下代码将python列表转换为数据帧 from pyspark.sql.types import StructType from pyspark.sql.types import StructField from pyspark.sql.types import StringType, IntegerType schema = StructType([StructField("value", IntegerType(), True)]) my_list = [1,

这个案例非常简单,我需要使用以下代码将python列表转换为数据帧

from pyspark.sql.types import StructType
from pyspark.sql.types import StructField
from pyspark.sql.types import StringType, IntegerType

schema = StructType([StructField("value", IntegerType(), True)])
my_list = [1, 2, 3, 4]
rdd = sc.parallelize(my_list)
df = sqlContext.createDataFrame(rdd, schema)

df.show()
失败,错误如下:

    raise TypeError("StructType can not accept object %r in type %s" % (obj, type(obj)))
TypeError: StructType can not accept object 1 in type <class 'int'>
raise TypeError(“StructType无法接受类型%s”%中的对象%r(obj,类型(obj)))
类型错误:StructType无法接受类型中的对象1

请参见以下代码:

    from pyspark.sql import Row
    li=[1,2,3,4]
    rdd1 = sc.parallelize(li)
    row_rdd = rdd1.map(lambda x: Row(x))
    df=sqlContext.createDataFrame(row_rdd,['numbers']).show()
df

+-------+
|numbers|
+-------+
|      1|
|      2|
|      3|
|      4|
+-------+

此解决方案也是一种使用更少代码、避免对RDD进行序列化并且可能更容易理解的方法:

from pyspark.sql.types import IntegerType

# notice the variable name (more below)
mylist = [1, 2, 3, 4]

# notice the parens after the type name
spark.createDataFrame(mylist, IntegerType()).show()

注意:关于命名变量
list
:术语
list
是一个Python内置函数,因此强烈建议我们避免使用内置名称作为变量的名称/标签,因为我们最终会覆盖
list()
函数之类的内容。当快速而肮脏地创建原型时,许多人会使用类似这样的东西:
mylist

谢谢你的快速回答,这很有效,但我想了解你的方法和我的方法有什么不同?在您的代码中,您将每个RDD项转换为一行,而我的代码没有这样做,是不是这样我的代码失败了?是的,要将列表读入数据帧,您必须将其转换为行。从这里可以直接读取为数据帧。如果问题得到解决,请接受答案。您可以参考此链接了解更多详细信息。明白了,非常感谢!在upvote和downvote下面应该有右键。只需单击它。您的代码失败,因为架构与数据不匹配。根据上面链接的问题。这是更好的答案,因为它避免了对
rdd
的序列化。谢谢这是一个很好的答案!回答得好,回答得清楚。最后一行是
.show()
使
df
保持
None
。是否有任何方法为数据字段命名(在本例中默认为“value”)更改默认列名
也让我感到困惑。我有一个方法可以让它重命名:
tmp\u df.selectExpr(“值为文本”)
有人有更好的主意吗?