美文网首页
Pandas实战——Series

Pandas实战——Series

作者: 深思海数_willschang | 来源:发表于2021-09-09 11:13 被阅读0次
pandas in action.png

原书地址

本篇内容为原书第二,三章节的笔记。

  • The Series Object
  • Series Methods

Series可谓是pandas里的一个重要的数据结构,是带标签的一维数组,可存储整数、浮点数、字符串、Python 对象等类型的数据。轴标签统称为索引,主要由两部分组成:

  • values:一组数据(ndarray类型)
  • index:相关的数据索引标签

    Index labels can be of any immutable data type: strings, tuples, datetimes, and more.

Series

在Jupyter notebook下,可通过shift+tab来查看函数的定义等信息。


shift+tab

创建Series

pd.Series(
    data=None,
    index=None,
    dtype=None,
    name=None,
    copy=False,
    fastpath=False,
)

上面为Series的参数,在一般情况下我们主要通过data来传递数据进行Series创建。在需要的场合下,各参数也是可以进行配置,如索引值,数据类型等。


set index
  • 通过list数据进行Series实例


    Series by list data
  • 指定数据类型dtype


    dtype
  • 含有缺失值情况(missing values)
    nan:not a number


    with nan

Pandas automatically converts numeric values from integers to floating-points when it spots a nan value.

  • python数据进行Series创建,字典,元组等
python objects

注:集合(set)是无序状态的数据集,在pandas中我们无法直接进行Series创建,需要将其先转换为其他格式再进行Series创建。

Series属性

  • .values
  • .index
  • .dtype
  • .size
  • .shape
  • .is_unique
  • .is_monotonic # 是否是单调递增数据组

Series方法

统计计算方法
  • .head()
  • .tail()
  • .count()
  • .sum() # .sum(skipna=False) 计算时不跳过空值
  • .product()
  • .comsum()
  • .pct_change()

pct_change(fill_method='bfill'),fill_method有bfill(backfill)或pad(ffill)值,分别表示空值的填充为向后取值,或是向前取值

pct_change(fill_method='ffill') pct_change(fill_method='bfill').png
  • .mean()
  • .median()
  • .std()
  • .max()
  • .min()
  • .describe() # 数据全况,个数,均值方差等
  • .sample(n) # 随机返回数据
  • .unique() # 去重
  • .nunique() # 唯一值个数

算术操作方法

  • .add() # +
  • .sub() 或 .subtract()
  • .mul() 或 multiply()
  • .div() 或 divide()
  • s1 // 4 或s1.floordiv(4)
  • s1 % 3 或 s1.mod(3)

广播机制 Broadcasting

image.png image.png

Series方法

这小节是原书第三章内容,主要介绍一些series的操作方法,如排序,计数和分组等。

read_csv()取得数据并构建Series数据

index_col && squeeze.png parse_dates usecols
  • 排序操作
    对值(sort_values)或索引(sort_index)进行排序。


    sort_values
sort_index
  • 丢弃空值 dropna()


    dropna
  • 最大最小值 nlargest()/nsmallest()


    nlargest
  • 统计个数 value_counts()


    value_counts
  • 取得Series里的最大最小值


    max-min
bin区间段计数
通过函数对数据进行操作,apply()
apply() chain operation

相关文章

网友评论

      本文标题:Pandas实战——Series

      本文链接:https://www.haomeiwen.com/subject/ajliwltx.html