如何从多个列表创建 PySpark DataFrame?


PySpark 是一个功能强大的工具,用于在分布式计算环境中处理大型数据集。数据分析中的一个基本任务是将数据转换为易于处理和分析的格式。在 PySpark 中,数据通常存储在 DataFrame 中,DataFrame 是一个组织成命名列的分布式数据集合。

在某些情况下,我们可能希望从多个列表创建 PySpark DataFrame。当我们的数据格式不容易从文件或数据库加载时,这很有用。例如,我们可能有一些存储在 Python 列表或 NumPy 数组中的数据,我们希望将其转换为 PySpark DataFrame 以进行进一步分析。

在本文中,我们将探讨如何从多个列表创建 PySpark DataFrame。我们将讨论不同的方法,并为每种方法提供带有注释和输出的代码示例。

将列表转换为 NumPy 数组,然后转换为 PySpark DataFrame

从多个列表创建 PySpark DataFrame 的一种方法是,首先将列表转换为 NumPy 数组,然后使用 createDataFrame() 函数从 NumPy 数组创建 PySpark DataFrame。此方法需要 pyspark.sql.types 模块来指定 DataFrame 的模式。

请考虑以下代码。

示例

import numpy as np
from pyspark.sql.types import StructType, StructField, IntegerType

# Define the lists
age = [20, 25, 30, 35, 40]
salary = [25000, 35000, 45000, 55000, 65000]

# Convert the lists to a NumPy array
data = np.array([age, salary]).T

# Define the schema
schema = StructType([
	StructField("age", IntegerType(), True),
	StructField("salary", IntegerType(), True)
])

# Create the PySpark DataFrame
df = spark.createDataFrame(data.tolist(), schema=schema)

# Show the DataFrame
df.show()

解释

  • 首先,我们导入所需的模块 - numpy 和 pyspark.sql.types。

  • 接下来,我们定义两个列表:age 和 salary。

  • 然后,我们使用 np.array() 函数将列表转换为 NumPy 数组,并使用 .T 转置数组。

  • 之后,我们使用 StructType() 和 StructField() 函数定义 DataFrame 的模式。在本例中,我们定义了两个列 - age 和 salary - 数据类型为 IntegerType()。

  • 最后,我们使用 createDataFrame() 函数创建 PySpark DataFrame,并将转换为列表的 NumPy 数组和模式作为参数传递。然后,我们使用 show() 函数显示 DataFrame。

输出

+---+------+
|age|salary|
+---+------+
| 20| 25000|
| 25| 35000|
| 30| 45000|
| 35| 55000|
| 40| 65000|
+---+------+

使用 PySpark 的 createDataFrame() 方法

在这种方法中,我们将使用 PySpark 提供的 createDataFrame() 方法直接从列表创建 PySpark DataFrame。我们将首先创建一个元组列表,其中每个元组表示 DataFrame 中的一行。然后,我们将创建一个模式来定义 DataFrame 的结构,即列名和数据类型。最后,我们将通过将元组列表和模式作为参数传递给 createDataFrame() 方法来创建一个 DataFrame。

请考虑以下代码。

示例

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

# Initialize SparkSession
spark = SparkSession.builder.appName("Create DataFrame from Lists").getOrCreate()

# Define the data as lists
names = ["Alice", "Bob", "Charlie", "David"]
ages = [25, 30, 35, 40]
genders = ["Female", "Male", "Male", "Male"]

# Define the schema of the dataframe
schema = StructType([
	StructField("Name", StringType(), True),
	StructField("Age", IntegerType(), True),
	StructField("Gender", StringType(), True)
])

# Create a list of tuples
data = [(names[i], ages[i], genders[i]) for i in range(len(names))]

# Create a PySpark dataframe
df = spark.createDataFrame(data, schema)

# Show the dataframe
df.show()

解释

  • 首先,我们导入所需的模块 - numpy 和 pyspark.sql.types。

  • 接下来,我们定义两个列表:age 和 salary。

  • 然后,我们使用 np.array() 函数将列表转换为 NumPy 数组,并使用 .T 转置数组。

  • 之后,我们使用 StructType() 和 StructField() 函数定义 DataFrame 的模式。在本例中,我们定义了两个列 - age 和 salary - 数据类型为 IntegerType()。

  • 最后,我们使用 createDataFrame() 函数创建 PySpark DataFrame,并将转换为列表的 NumPy 数组和模式作为参数传递。然后,我们使用 show() 函数显示 DataFrame。

输出

+-------+---+---------------+
| Name   |Age|   Gender|
+-------+---+----------------+
|  Alice    |  25  |  Female |
|  Bob      |  30  |  Male   |
|  Charlie  |  35  |  Male   |
|  David    |  40  |  Male   |
+-------+---+---------------+

结论

在本文中,我们探讨了两种从多个列表创建 PySpark DataFrame 的不同方法。第一种方法使用 Row() 函数创建数据行,然后使用 createDataFrame() 方法创建 DataFrame。第二种方法使用 StructType() 和 StructField() 函数定义模式,然后使用 createDataFrame() 方法以及数据和模式作为参数创建 DataFrame。

更新于: 2023年8月3日

2K+ 次查看

开启你的 职业生涯

通过完成课程获得认证

开始学习
广告

© . All rights reserved.