891 字
4 分钟
阅读量加载中...
polars学习-05_包含的数据结构

背景#

polars学习系列文章,第5篇 包含的数据结构,与 pandas 一样,polars 包含的数据结构是:SeriesDataFrame,大部分操作与pandas 保持一致,减少了大家的学习难度

该系列文章会分享到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

Series 数据列#

Series 是一维的数据结构,其有相同的数据类型,可以理解为数据库中的一列

import polars as pl
s = pl.Series("a", [1, 2, 3, 4, 5])
print(s)
shape: (5,)
Series: 'a' [i64]
[
1
2
3
4
5
]

数据列的操作#

print(s.min()) #1
print(s.max()) #5
print(s.mean()) #3.0
print(s.count()) #5

DataFrame 数据框#

DataFrame 是一个二维的数据结构,其是由一系列的 Series 组成,可以理解为一张数据表,包含很多列

from datetime import datetime
df = pl.DataFrame(
{
"integer": [1, 2, 3, 4, 5],
"date": [
datetime(2022, 1, 1),
datetime(2022, 1, 2),
datetime(2022, 1, 3),
datetime(2022, 1, 4),
datetime(2022, 1, 5),
],
"float": [4.0, 5.0, 6.0, 7.0, 8.0],
}
)
print(df)
shape: (5, 3)
┌─────────┬─────────────────────┬───────┐
│ integer ┆ date ┆ float
---------
│ i64 ┆ datetime[μs] ┆ f64 │
╞═════════╪═════════════════════╪═══════╡
12022-01-01 00:00:004.0
22022-01-02 00:00:005.0
32022-01-03 00:00:006.0
42022-01-04 00:00:007.0
52022-01-05 00:00:008.0
└─────────┴─────────────────────┴───────┘

默认展示前5行数据,也可以传出要展示的行数

print(df.head())
#shape: (5, 3)
┌─────────┬─────────────────────┬───────┐
│ integer ┆ date ┆ float
---------
│ i64 ┆ datetime[μs] ┆ f64 │
╞═════════╪═════════════════════╪═══════╡
12022-01-01 00:00:004.0
22022-01-02 00:00:005.0
32022-01-03 00:00:006.0
42022-01-04 00:00:007.0
52022-01-05 00:00:008.0
└─────────┴─────────────────────┴───────┘
print(df.head(3))
#shape: (3, 3)
┌─────────┬─────────────────────┬───────┐
│ integer ┆ date ┆ float
---------
│ i64 ┆ datetime[μs] ┆ f64 │
╞═════════╪═════════════════════╪═══════╡
12022-01-01 00:00:004.0
22022-01-02 00:00:005.0
32022-01-03 00:00:006.0
└─────────┴─────────────────────┴───────┘

Tail#

默认展示最后5行数据,也可以传出要展示的行数

print(df.tail())
#shape: (5, 3)
┌─────────┬─────────────────────┬───────┐
│ integer ┆ date ┆ float
---------
│ i64 ┆ datetime[μs] ┆ f64 │
╞═════════╪═════════════════════╪═══════╡
12022-01-01 00:00:004.0
22022-01-02 00:00:005.0
32022-01-03 00:00:006.0
42022-01-04 00:00:007.0
52022-01-05 00:00:008.0
└─────────┴─────────────────────┴───────┘
print(df.tail(3))
#shape: (3, 3)
┌─────────┬─────────────────────┬───────┐
│ integer ┆ date ┆ float
---------
│ i64 ┆ datetime[μs] ┆ f64 │
╞═════════╪═════════════════════╪═══════╡
32022-01-03 00:00:006.0
42022-01-04 00:00:007.0
52022-01-05 00:00:008.0
└─────────┴─────────────────────┴───────┘

Sample 随机抽样#

print(df.sample(3))
#shape: (3, 3)
┌─────────┬─────────────────────┬───────┐
│ integer ┆ date ┆ float
---------
│ i64 ┆ datetime[μs] ┆ f64 │
╞═════════╪═════════════════════╪═══════╡
52022-01-05 00:00:008.0
32022-01-03 00:00:006.0
12022-01-01 00:00:004.0
└─────────┴─────────────────────┴───────┘

Describe 数据概况#

print(df.describe())
#shape: (9, 4)
┌────────────┬──────────┬─────────────────────┬──────────┐
│ statistic ┆ integer ┆ date ┆ float
------------
str ┆ f64 ┆ str ┆ f64 │
╞════════════╪══════════╪═════════════════════╪══════════╡
│ count ┆ 5.055.0
│ null_count ┆ 0.000.0
│ mean ┆ 3.02022-01-03 00:00:006.0
│ std ┆ 1.581139 ┆ null ┆ 1.581139
min1.02022-01-01 00:00:004.0
25%2.02022-01-02 00:00:005.0
50%3.02022-01-03 00:00:006.0
75%4.02022-01-04 00:00:007.0
max5.02022-01-05 00:00:008.0
└────────────┴──────────┴─────────────────────┴──────────┘

历史相关文章#


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

微信公众号 QQ群