如何自己做彩票网站,网站搜索引擎优化方案范文,导游网站如何建设的,深圳网站建设_背景
polars学习系列文章#xff0c;第7篇 缺失值
该系列文章会分享到github#xff0c;大家可以去下载jupyter文件#xff0c;进行参考学习 仓库地址#xff1a;https://github.com/DataShare-duo/polars_learn
小编运行环境
import sysprint(python 版本#xff1a;…背景
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.22polars 中缺失值的定义
在 polars 中缺失值用 null 来表示只有这1种表示方式这个与 pandas 不同在 pandas 中 NaNNotaNumber也代表是缺失值但在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 │
╞══════╪══════╡
│ 1 ┆ 1 │
│ 2 ┆ null │
│ 3 ┆ 3 │
└──────┴──────┘常量填充
fill_literal_df df.with_columns(fillpl.col(col2).fill_null(pl.lit(2)),
)
print(fill_literal_df)
#shape: (3, 3)
┌──────┬──────┬──────┐
│ col1 ┆ col2 ┆ fill │
│ --- ┆ --- ┆ --- │
│ i64 ┆ i64 ┆ i64 │
╞══════╪══════╪══════╡
│ 1 ┆ 1 ┆ 1 │
│ 2 ┆ null ┆ 2 │
│ 3 ┆ 3 ┆ 3 │
└──────┴──────┴──────┘填充策略
填充策略{‘forward’, ‘backward’, ‘min’, ‘max’, ‘mean’, ‘zero’, ‘one’}
fill_df df.with_columns(forwardpl.col(col2).fill_null(strategyforward),backwardpl.col(col2).fill_null(strategybackward),
)
print(fill_df)
#shape: (3, 4)
┌──────┬──────┬─────────┬──────────┐
│ col1 ┆ col2 ┆ forward ┆ backward │
│ --- ┆ --- ┆ --- ┆ --- │
│ i64 ┆ i64 ┆ i64 ┆ i64 │
╞══════╪══════╪═════════╪══════════╡
│ 1 ┆ 1 ┆ 1 ┆ 1 │
│ 2 ┆ null ┆ 1 ┆ 3 │
│ 3 ┆ 3 ┆ 3 ┆ 3 │
└──────┴──────┴─────────┴──────────┘通过表达式
fill_median_df df.with_columns(fillpl.col(col2).fill_null(pl.median(col2)), #类型会转换为浮点型
)
print(fill_median_df)
#shape: (3, 3)
┌──────┬──────┬──────┐
│ col1 ┆ col2 ┆ fill │
│ --- ┆ --- ┆ --- │
│ i64 ┆ i64 ┆ f64 │
╞══════╪══════╪══════╡
│ 1 ┆ 1 ┆ 1.0 │
│ 2 ┆ null ┆ 2.0 │
│ 3 ┆ 3 ┆ 3.0 │
└──────┴──────┴──────┘通过插值法
fill_interpolation_df df.with_columns(fillpl.col(col2).interpolate(),
)
print(fill_interpolation_df)
#shape: (3, 3)
┌──────┬──────┬──────┐
│ col1 ┆ col2 ┆ fill │
│ --- ┆ --- ┆ --- │
│ i64 ┆ i64 ┆ f64 │
╞══════╪══════╪══════╡
│ 1 ┆ 1 ┆ 1.0 │
│ 2 ┆ null ┆ 2.0 │
│ 3 ┆ 3 ┆ 3.0 │
└──────┴──────┴──────┘历史相关文章
Python polars学习-01 读取与写入文件Python polars学习-02 上下文与表达式polars学习-03 数据类型转换Python polars学习-04 字符串数据处理Python polars学习-05 包含的数据结构Python polars学习-06 Lazy / Eager API 以上是自己实践中遇到的一些问题分享出来供大家参考学习欢迎关注微信公众号DataShare 不定期分享干货