如何在pyspark中创建嵌套列表?

2024-09-27 17:31:33 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要创建嵌套列表。我的txt数据是

(电话号码、时间、三角洲时间、纬度、经度)

... 
0544144,23,86,40.761650,29.940929
0544147,23,104,40.768749,29.968599
0545525,20,86,40.761650,29.940929
0538333,21,184,40.764679,29.929543
05477900,21,204,40.773071,29.975010
0561554,23,47,40.764694,29.927397
...

我的代码也是

^{pr2}$

如您所见,每列都有一个平均值;telophone、time、delta time等,但每一行都必须使用一个列表。 如果我想看第一个电话号码

print tel0_list[0]

输入:

0544144

它同样有效。我需要用它来创建每一行。在

例如

Data[]列表可以是每行的lıst。如果我想看到数据[1],我的输入必须像

(0544147,23,104,40.768749,29.968599)

我要怎么做?在

谢谢


Tags: 数据代码txt列表time时间电话号码平均值
1条回答
网友
1楼 · 发布于 2024-09-27 17:31:33

由于文本文件是csv格式,因此如果使用Spark 2.x,可以轻松地将其加载到数据帧中:

from pyspark.sql import SparkSession
from pyspark.sql.types import StructType, StructField, IntegerType, DoubleType

spark = SparkSession.builder.getOrCreate()

schema = StructType([
            StructField("tel", IntegerType(), True),
            StructField("time", IntegerType(), True),
            StructField("deltatime", IntegerType(), True),
            StructField("lat", DoubleType(), True),
            StructField("long", DoubleType(), True)
        ])

data = spark.read.csv("data2.txt", header=False, schema=schema)

然后您可以通过以下方式访问数据:

^{pr2}$

注意:在Spark中访问数据[1]没有任何意义,因为它是一个分布式系统。在

相关问题 更多 >

    热门问题