
本文旨在指导用户如何在polars中高效地将包含列表的dataframe列转换为更规整的宽格式。通过结合`unpivot`、`list.to_struct`和`unnest`等核心操作,我们能够将原始列名转换为新的标识列,并将列表中的元素展开为独立的数值列,从而实现复杂数据的结构化重塑,提升数据分析的便利性与效率。
在数据处理和分析中,我们经常会遇到需要对DataFrame的列进行复杂重塑的情况,特别是当列中包含列表(List)类型的数据时。本教程将详细介绍如何利用Polars的强大功能,将一个包含列表列的DataFrame转换为一种更易于分析的宽格式,其中原始列名被转换为新的标识列,而列表中的元素则被展开为独立的数值列。
假设我们有一个Polars DataFrame,其中包含多列,每列的值都是一个整数列表。我们的目标是将其转换为一种格式,使得:
以下是我们的初始DataFrame示例:
import polars as pl
df = pl.DataFrame({
"foo": [[1, 2, 3], [7, 8, 9]],
"bar": [[4, 5, 6], [1, 0, 1]]
})
print("原始 DataFrame:")
print(df)输出:
原始 DataFrame: shape: (2, 2) ┌───────────┬───────────┐ │ foo ┆ bar │ │ --- ┆ --- │ │ list[i64] ┆ list[i64] │ ╞═══════════╪═══════════╡ │ [1, 2, 3] ┆ [4, 5, 6] │ │ [7, 8, 9] ┆ [1, 0, 1] │ └───────────┴───────────┘
我们期望的输出是这样的:
shape: (4, 4) ┌──────┬────────┬────────┬────────┐ │ Name ┆ Value0 ┆ Value1 ┆ Value2 │ │ --- ┆ --- ┆ --- ┆ --- │ │ str ┆ i64 ┆ i64 ┆ i64 │ ╞══════╪════════╪════════╪════════╡ │ foo ┆ 1 ┆ 2 ┆ 3 │ │ foo ┆ 7 ┆ 8 ┆ 9 │ │ bar ┆ 4 ┆ 5 ┆ 6 │ │ bar ┆ 1 ┆ 0 ┆ 1 │ └──────┴────────┴────────┴────────┘
要实现上述转换,我们需要依次使用Polars的三个关键操作:unpivot、list.to_struct 和 unnest。
unpivot 操作可以将DataFrame的多个列“旋转”成长格式。它会将指定的列名转换为一个新列的值,同时将这些列的原始值放入另一个新列中。
在这个场景中,我们将所有的列("foo", "bar")都进行 unpivot 操作。variable_name 参数用于指定存储原始列名的新列的名称,而 value_name 则指定存储原始值的新列的名称(默认为 "value")。
# 步骤 1: unpivot
df_unpivoted = df.unpivot(variable_name="Name")
print("\n步骤 1 - unpivot 后的 DataFrame:")
print(df_unpivoted)输出:
步骤 1 - unpivot 后的 DataFrame: shape: (4, 2) ┌──────┬───────────┐ │ Name ┆ value │ │ --- ┆ --- │ │ str ┆ list[i64] │ ╞══════╪═══════════╡ │ foo ┆ [1, 2, 3] │ │ foo ┆ [7, 8, 9] │ │ bar ┆ [4, 5, 6] │ │ bar ┆ [1, 0, 1] │ └──────┴───────────┘
现在,我们有了一个 Name 列,其中包含原始列名,以及一个 value 列,其中包含对应的列表数据。
接下来,我们需要将 value 列中的每个列表转换为一个结构体(Struct),这样列表中的每个元素都可以被命名为一个字段。list.to_struct() 方法非常适合此目的。fields 参数允许我们定义结构体中各个字段的名称。这里我们使用一个 lambda 函数来动态生成字段名,例如 "Value0", "Value1", "Value2"。
# 步骤 2: 将列表转换为结构体
df_struct = df_unpivoted.with_columns(
pl.col("value").list.to_struct(fields=lambda x: f"Value{x}")
)
print("\n步骤 2 - 转换为结构体后的 DataFrame:")
print(df_struct)输出:
步骤 2 - 转换为结构体后的 DataFrame:
shape: (4, 2)
┌──────┬───────────────────────────┐
│ Name ┆ value │
│ --- ┆ --- │
│ str ┆ struct[3] │
╞══════╪═══════════════════════════╡
│ foo ┆ {1,2,3} │
│ foo ┆ {7,8,9} │
│ bar ┆ {4,5,6} │
│ bar ┆ {1,0,1} │
└──────┴───────────────────────────┘现在,value 列的类型已经变成了 struct,并且内部包含了三个字段。
最后一步是使用 unnest 操作。unnest 会将一个结构体列展开,使其内部的每个字段都成为DataFrame中的一个独立列。这正是我们想要实现的目标。
# 步骤 3: 展开结构体列
df_final = df_struct.unnest("value")
print("\n步骤 3 - unnest 后的最终 DataFrame:")
print(df_final)输出:
步骤 3 - unnest 后的最终 DataFrame: shape: (4, 4) ┌──────┬────────┬────────┬────────┐ │ Name ┆ Value0 ┆ Value1 ┆ Value2 │ │ --- ┆ --- ┆ --- ┆ --- │ │ str ┆ i64 ┆ i64 ┆ i64 │ ╞══════╪════════╪════════╪════════╡ │ foo ┆ 1 ┆ 2 ┆ 3 │ │ foo ┆ 7 ┆ 8 ┆ 9 │ │ bar ┆ 4 ┆ 5 ┆ 6 │ │ bar ┆ 1 ┆ 0 ┆ 1 │ └──────┴────────┴────────┴────────┘
至此,我们成功将原始的DataFrame转换成了目标格式。
为了提高效率和代码的可读性,我们可以将这些操作通过方法链(method chaining)的方式组合在一起:
import polars as pl
# 初始 DataFrame
df = pl.DataFrame({
"foo": [[1, 2, 3], [7, 8, 9]],
"bar": [[4, 5, 6], [1, 0, 1]]
})
# 链式操作实现转换
output_df = (
df
.unpivot(variable_name="Name") # 将列名转换为 "Name" 列的值
.with_columns(pl.col("value").list.to_struct(fields=lambda x: f"Value{x}")) # 将列表转换为结构体
.unnest("value") # 展开结构体列
)
print("\n最终转换结果:")
print(output_df)通过本教程,您应该已经掌握了在Polars中将包含列表的列转换为宽格式的有效方法。这种技术在处理嵌套数据或需要将“长”格式数据转换为“宽”格式以进行进一步分析时非常有用。
以上就是Polars中复杂列的结构化转换:从列表到宽格式的实践指南的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号