当前位置: 首页 > news >正文

Python polars学习-07 缺失值

背景

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

该系列文章会分享到github,大家可以去下载jupyter文件,进行参考学习
仓库地址:https://github.com/DataShare-duo/polars_learn

小编运行环境

import sysprint('python 版本:',sys.version.split('|')[0])
#python 版本: 3.11.9import polars as plprint("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  │
└──────┴──────┴──────┘

历史相关文章

  • Python polars学习-01 读取与写入文件
  • Python polars学习-02 上下文与表达式
  • polars学习-03 数据类型转换
  • Python polars学习-04 字符串数据处理
  • Python polars学习-05 包含的数据结构
  • Python polars学习-06 Lazy / Eager API

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

http://www.lryc.cn/news/382993.html

相关文章:

  • 前端面试题(八)答案版
  • 在交易中出场比入场更为重要
  • 【D3.js in Action 3 精译】关于本书
  • 【408考点之数据结构】二叉树的概念与实现
  • STM32之二:时钟树
  • 第十四站:Java玫瑰金——移动开发(第二篇)
  • 数据处理技术影响皮质-皮质间诱发电位的量化
  • ResultSet的作用和类型
  • 计算机网络:运输层 - TCP首部格式 连接的创建与释放
  • 妈耶!被夸爆的零售数据分析方案在这里
  • AI探索:最佳落地应用场景
  • 2024年最新机动车签字授权人考试题库。
  • 软RAID
  • IDEA 学习之 启动“卡死”
  • 豆瓣高分项目管理书籍推荐
  • 关于docker存储overlay2相关问题
  • 实现批量自动化电商数据采集|商品详情页面|店铺商品信息|订单详情数据
  • ES6(ECMAScript 6.0) 新特性
  • 性能工具之 JMeter 常用组件介绍(八)
  • 分布式锁(Redission)
  • 【ARMv8/v9 GIC 系列 3 -- GIC 的 类型寄存器 GICD_TYPER】
  • MATLAB算法实战应用案例精讲-【数模应用】线性判别分析(附MATLAB、python和R语言代码实现)
  • 打造智能家居:用ESP32轻松实现无线控制与环境监测
  • 大型Web应用的模块化与组织实践:Flask Blueprints深入解析
  • AI 智算产业发展现状和预测报告
  • 【软件工具】Xshell安装教程
  • git如何切换到tag分支
  • 【启明智显产品介绍】Model3C工业级HMI芯片详解专题(三)通信接口
  • Mysql实战中的一些小tips
  • 【Linux】使用信号进行进程间通信