• Pandas里的Series学习


    python里有列表,很强大,pandas里的Series更强大,它是dataframe的基石。

    Series是什么?

    它可以表示表格中的一列数据(理论上不太严谨,表格里的一行也是Series),相当于一维数组,它最强大的地方在于它可以通过标签快速找到它,也称为索引。

    通过列表构建一个Series

    import pandas as pd
    
    # 语文,数学,英语
    score = pd.Series([99, 120, 114])
    print(score)
    
    • 1
    • 2
    • 3
    • 4
    • 5

    输出:

    0     99
    1    120
    2    114
    dtype: int64
    
    • 1
    • 2
    • 3
    • 4

    指定索引

    三科的成绩可以用不同的标签来索引它。

    import pandas as pd
    
    # 语文,数学,英语
    score = pd.Series([99, 120, 114], index=['cn', 'math', 'en'])
    print(score)
    
    • 1
    • 2
    • 3
    • 4
    • 5

    输出:

    cn       99
    math    120
    en      114
    dtype: int64
    
    • 1
    • 2
    • 3
    • 4

    dtype数据类型

    Series里的所有数据的类型必须是一致的,构建的时候系统会自动猜出一种数据类型,上面的例子里是int64,但成绩可能是小数,所以可以指定dtype参数。

    这里需要导入numpy,用到np.float32。

    import pandas as pd
    import numpy as np
    
    # 语文,数学,英语
    score = pd.Series([99, 120, 114], index=['cn', 'math', 'en'], dtype=np.float32)
    print(score)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    输出:

    cn       99.0
    math    120.0
    en      114.0
    dtype: float32
    
    • 1
    • 2
    • 3
    • 4

    如果直接用小数构建Series,则dtype类型为float64。

    score = pd.Series([99.0, 120.0, 114.0], index=['cn', 'math', 'en'])
    
    • 1

    输出:

    cn       99.0
    math    120.0
    en      114.0
    dtype: float64
    
    • 1
    • 2
    • 3
    • 4

    访问元素

    与python里的列表一样。可以根据位置访问,还可以根据标签访问。

    print(score[0])
    print(score['cn'])
    print(score['math'])
    print(score['en'])
    
    • 1
    • 2
    • 3
    • 4

    从字典构建Series

    字典里的key就是索引,value就是Series的值。

    score = pd.Series({'cn':99.0, 'math':120.0, 'en':114.0})
    
    • 1

    需要注意的是:字典里的key是不允许重复的,但Series里的索引可以重复。

    索引index、值values

    score = pd.Series({'cn':99.0, 'math':120.0, 'en':114.0})
    print(score.index)
    print(score.values)
    
    • 1
    • 2
    • 3

    输出:

    Index(['cn', 'math', 'en'], dtype='object')
    [ 99. 120. 114.]
    
    • 1
    • 2

    从numpy构建Series

    arr = np.random.random(size=10)
    ser = pd.Series(arr)
    print(ser)
    
    • 1
    • 2
    • 3

    输出:

    0    0.112506
    1    0.507774
    2    0.732660
    3    0.480606
    4    0.944777
    5    0.777633
    6    0.682351
    7    0.011652
    8    0.893980
    9    0.971912
    dtype: float64
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    切片

    可以像python里根据位置来切片,注意范围是开区间。
    也可以用标签来切片,这里是闭区间,即冒号前后的元素都包含在内。

    score = pd.Series({'cn':99.0, 'math':120.0, 'en':114.0})
    print(score[0:2])
    print(score['cn':'math'])
    
    • 1
    • 2
    • 3

    输出2遍:

    cn       99.0
    math    120.0
    dtype: float64
    
    • 1
    • 2
    • 3

    元素的个数

    score = pd.Series({'cn':99.0, 'math':120.0, 'en':114.0})
    print(len(score))
    print(score.size)
    print(score.shape)
    
    • 1
    • 2
    • 3
    • 4

    输出:

    3
    3
    (3,)
    
    • 1
    • 2
    • 3

    注意这里的shape返回多维数组的每一个维度。

    条件筛选

    在索引的方括号里还可以直接写条件表达式,将满足条件的数据直接取出来。

    score = pd.Series({'cn':99.0, 'math':120.0, 'en':114.0})
    print(score[score > 100])
    
    • 1
    • 2

    输出:

    math    120.0
    en      114.0
    dtype: float64
    
    • 1
    • 2
    • 3

    运算

    python里列表的加法是concat,而Series里的运算则是向量运算。

    score = pd.Series({'cn':99.0, 'math':120.0, 'en':114.0})
    score2 = score - 10.0
    print(score2)
    
    • 1
    • 2
    • 3

    输出:

    cn       89.0
    math    110.0
    en      104.0
    dtype: float64
    
    • 1
    • 2
    • 3
    • 4

    两个Series之间的运算则是根据标签来合并的,如果某个标签没有值,则显示NaN(缺失值not a number)。

    score = pd.Series({'cn':99.0, 'math':120.0, 'en':114.0})
    score2 = score + pd.Series({'cn':10.0, 'math':0, 'phy':70.0})
    print(score2)
    
    • 1
    • 2
    • 3

    输出:

    cn      109.0
    en        NaN
    math    120.0
    phy       NaN
    dtype: float64
    
    • 1
    • 2
    • 3
    • 4
    • 5

    判断是否有重复元素

    下面的代码里,a没有重复元素,b里的元素1有重复。

    a = pd.Series([1, 2, 3, 4, 5])
    b = pd.Series([1, 1, 3, 4, 5])
    print(a.is_unique)
    print(b.is_unique)
    
    • 1
    • 2
    • 3
    • 4

    输出:

    True
    False
    
    • 1
    • 2

    修改元素

    下面的代码增加了物理成绩。

    score = pd.Series({'cn':99.0, 'math':120.0, 'en':114.0})
    score['phy'] = 70.0
    print(score)
    
    • 1
    • 2
    • 3

    输出:

    cn       99.0
    math    120.0
    en      114.0
    phy      70.0
    dtype: float64
    
    • 1
    • 2
    • 3
    • 4
    • 5

    也可以用切片修改多个元素。

    score = pd.Series({'cn':99.0, 'math':120.0, 'en':114.0})
    score['cn':'math'] = 0
    print(score)
    
    • 1
    • 2
    • 3

    输出:

    cn        0.0
    math      0.0
    en      114.0
    dtype: float64
    
    • 1
    • 2
    • 3
    • 4

    添加元素

    score1 = pd.Series({'cn':99.0, 'math':120.0, 'en':114.0})
    score2 = pd.Series({'phy':70, 'chem':66})
    score3 = pd.concat([score1, score2])
    print(score3)
    
    • 1
    • 2
    • 3
    • 4

    输出:

    cn       99.0
    math    120.0
    en      114.0
    phy      70.0
    chem     66.0
    dtype: float64
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    删除元素

    用python里的del可以删除元素。另外一种办法是drop()函数。
    要注意drop()的默认行为,是返回一个删除了元素之后的对象,原来的Series并没有任何改变。

    score1 = pd.Series({'cn':99.0, 'math':120.0, 'en':114.0})
    score2 = score1.drop(index='cn')
    print(score1)
    print(score2)
    
    • 1
    • 2
    • 3
    • 4

    输出:

    cn       99.0
    math    120.0
    en      114.0
    dtype: float64
    math    120.0
    en      114.0
    dtype: float64
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    如果想在原来的Series上执行删除操作,则要指定inplace=True参数。

    score = pd.Series({'cn':99.0, 'math':120.0, 'en':114.0})
    score.drop(index=['cn','en'], inplace=True)
    print(score)
    
    • 1
    • 2
    • 3

    输出:

    math    120.0
    dtype: float64
    
    • 1
    • 2

    最大值、最小值

    找出最大的2个,最小的2个。

    ser = pd.Series([99, 120, 114, 70, 49.5, 130, 49, 48.5, 49.5, 60])
    print(ser.nlargest(2))
    print(ser.nsmallest(2))
    
    • 1
    • 2
    • 3

    输出:

    5    130.0
    1    120.0
    dtype: float64
    7    48.5
    6    49.0
    dtype: float64
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    统计元素的个数

    ser = pd.Series(['a','a','a','b','b','c'])
    print(ser.value_counts())
    
    • 1
    • 2

    输出:

    a    3
    b    2
    c    1
    dtype: int64
    
    • 1
    • 2
    • 3
    • 4
  • 相关阅读:
    Nodejs 安装与介绍
    游戏道具平台|基于Springboot+Vue实现游戏道具平台系统
    管理类联考——数学——汇总篇——知识点突破——数据分析——1. 计数原理——排列组合——公式
    Linux shell编程学习笔记8:使用字符串
    无偏估计和最小方差无偏估计简介
    Redis数据库角色:不只是缓存,还可以作为主数据库!
    谱定理等周边定理
    轻量封装WebGPU渲染系统示例<37>- 多个局部点光源应用于非金属材质形成的效果(源码)
    springboot+清远旅游推荐网站 毕业设计-附源码211551
    JavaScript switch语句
  • 原文地址:https://blog.csdn.net/slofslb/article/details/126082820