883 字
4 分钟
阅读量加载中...
polars学习-07_缺失值

背景#

polars学习系列文章,第7篇 缺失值

该系列文章会分享到github,大家可以去下载jupyter文件,进行参考学习

仓库地址:https://github.com/DataShare-duo/polars_learn

小编运行环境#

import sys
print('python 版本:',sys.version.split('|')[0])
#python 版本: 3.11.9
import polars as pl
print("polars 版本:",pl.__version__)
#polars 版本: 0.20.22

polars 中缺失值的定义#

在 polars 中缺失值用 null 来表示,只有这1种表示方式,这个与 pandas 不同,在 pandas 中 NaN(NotaNumber)也代表是缺失值,但在polars中把 NaN 归属为一种浮点数据

df = pl.DataFrame(
{
"value": [1,2,3, None,5,6,None,8,9],
},
)
print(df)
#shape: (9, 1)
┌───────┐
│ value │
---
│ i64 │
╞═══════╡
1
2
3
│ null │
5
6
│ null │
8
9
└───────┘

polars中缺失值包括的2种元信息#

  • 缺失值数量,可以通过 null_count 方法来快速获取,因为已经是计算好的,所以调用该方法会立即返回结果
  • 有效位图(validity bitmap),代表是否是缺失值,在内存中用 0 或 1 进行编码来表示,所占的内存空间非常小,通常占用空间为(数据框长度 / 8) bytes,通过 is_null 方法来查看数据是否是缺失值
null_count_df = df.null_count()
print(null_count_df)
#shape: (1, 1)
┌───────┐
│ value │
---
│ u32 │
╞═══════╡
2
└───────┘
is_null_series = df.select(
pl.col("value").is_null(),
)
print(is_null_series)
#shape: (9, 1)
┌───────┐
│ value │
---
bool
╞═══════╡
│ false │
│ false │
│ false │
│ true │
│ false │
│ false │
│ true │
│ false │
│ false │
└───────┘

缺失值填充#

缺失值填充主要通过 fill_null方法来处理,但是需求指定填充缺失值的方法

  • 常量,比如用 0 来填充
  • 填充策略,例如:向前、向后 等
  • 通过表达式,比如利用其他列来填充
  • 插值法
df = pl.DataFrame(
{
"col1": [1, 2, 3],
"col2": [1, None, 3],
},
)
print(df)
#shape: (3, 2)
┌──────┬──────┐
│ col1 ┆ col2 │
------
│ i64 ┆ i64 │
╞══════╪══════╡
11
2 ┆ null │
33
└──────┴──────┘

常量填充#

fill_literal_df = df.with_columns(
fill=pl.col("col2").fill_null(pl.lit(2)),
)
print(fill_literal_df)
#shape: (3, 3)
┌──────┬──────┬──────┐
│ col1 ┆ col2 ┆ fill │
---------
│ i64 ┆ i64 ┆ i64 │
╞══════╪══════╪══════╡
111
2 ┆ null ┆ 2
333
└──────┴──────┴──────┘

填充策略#

填充策略:{‘forward’, ‘backward’, ‘min’, ‘max’, ‘mean’, ‘zero’, ‘one’}

fill_df = df.with_columns(
forward=pl.col("col2").fill_null(strategy="forward"),
backward=pl.col("col2").fill_null(strategy="backward"),
)
print(fill_df)
#shape: (3, 4)
┌──────┬──────┬─────────┬──────────┐
│ col1 ┆ col2 ┆ forward ┆ backward │
------------
│ i64 ┆ i64 ┆ i64 ┆ i64 │
╞══════╪══════╪═════════╪══════════╡
1111
2 ┆ null ┆ 13
3333
└──────┴──────┴─────────┴──────────┘

通过表达式#

fill_median_df = df.with_columns(
fill=pl.col("col2").fill_null(pl.median("col2")), #类型会转换为浮点型
)
print(fill_median_df)
#shape: (3, 3)
┌──────┬──────┬──────┐
│ col1 ┆ col2 ┆ fill │
---------
│ i64 ┆ i64 ┆ f64 │
╞══════╪══════╪══════╡
111.0
2 ┆ null ┆ 2.0
333.0
└──────┴──────┴──────┘

通过插值法#

fill_interpolation_df = df.with_columns(
fill=pl.col("col2").interpolate(),
)
print(fill_interpolation_df)
#shape: (3, 3)
┌──────┬──────┬──────┐
│ col1 ┆ col2 ┆ fill │
---------
│ i64 ┆ i64 ┆ f64 │
╞══════╪══════╪══════╡
111.0
2 ┆ null ┆ 2.0
333.0
└──────┴──────┴──────┘

历史相关文章#


以上是自己实践中遇到的一些问题,分享出来供大家参考学习,欢迎关注微信公众号:DataShare ,不定期分享干货

微信公众号 QQ群