• pandas(Series二)


    pandas:Series数据对齐

    pandas在运算时,会按索引进行对齐然后计算,如果存在不同的索引,则结果的索引是两个操作数索引的并集

     1 In [1]: import pandas as pd
     2 
     3 In [2]: a = pd.Series([1,3,4], index=['a', 'b', 'c'])
     4 
     5 In [3]: b = pd.Series([5,6,7], index=['a', 'b', 't'])
     6 
     7 In [7]: a.values + b.values
     8 Out[7]: array([ 6,  9, 11])
     9 
    10 In [4]: a+b
    11 Out[4]:
    12 a    6.0
    13 b    9.0
    14 c    NaN
    15 t    NaN
    16 dtype: float64

    任何在两个Series对象相加时将缺失值设为0

    In [6]: a.add(b, fill_value=0)  # fill_value为缺失值赋值
    Out[6]:
    a    6.0
    b    9.0
    c    4.0
    t    7.0
    dtype: float64

    灵活的算术方法:add,sub,div.mul

    In [2]: a = pd.Series([1,3,4], index=['a', 'b', 'c'])
    
    In [3]: b = pd.Series([5,6,7], index=['a', 'b', 't'])
    In [5]: a.add(b)
    Out[5]:
    a    6.0
    b    9.0
    c    NaN
    t    NaN
    dtype: float64

    pandas:Series缺失数据

    缺失数据:使用NaN(Not a Number)来表示缺失数据,其值等于np.nan,内置的None值也会被当做NaN处理

    处理缺失值数据的相关方法

    In [10]: a = pd.Series([1,3,4], index=['a', 'b', 'c'])
    
    In [11]: b = pd.Series([5,6,7], index=['a', 'b', 't'])
    
    In [12]: a+b
    Out[12]:
    a    6.0
    b    9.0
    c    NaN
    t    NaN
    dtype: float64
    
    In [13]: c = _
    
    In [14]: c
    Out[14]:
    a    6.0
    b    9.0
    c    NaN
    t    NaN
    dtype: float64
    
    In [15]: c.dropna()  #dropna() 过滤掉值为NaN的行
    Out[15]:
    a    6.0
    b    9.0
    dtype: float64
    
    
    
    # 查看缺失值
    In [16]: c[c.isnull()]  # isnull放回布尔值数组,缺失值对应为True
    Out[16]:
    c   NaN
    t   NaN
    dtype: float64
    
    In [17]: c[~c.isnull()]  # 取非缺失值
    Out[17]:
    a    6.0
    b    9.0
    dtype: float64
    
    In [18]: c[c.notnull()]  # notnull返回布尔值数组,缺失值对应为False
    Out[18]:
    a    6.0
    b    9.0
    dtype: float64
    
    In [19]: c.isnull()
    Out[19]:
    a    False
    b    False
    c     True
    t     True
    dtype: bool
    
    In [20]: c.notnull()
    Out[20]:
    a     True
    b     True
    c    False
    t    False
    dtype: bool
    
    
    In [21]: c.fillna(0)  #fillna() 填充缺失值
    Out[21]:
    a    6.0
    b    9.0
    c    0.0
    t    0.0
    dtype: float64
    
    In [22]: c.fillna(c.mean())  # 平均值填充缺失值
    Out[22]:
    a    6.0
    b    9.0
    c    7.5
    t    7.5
    dtype: float64
  • 相关阅读:
    计算机的组成部分
    从LINQ开始之LINQ to Objects(下)
    从LINQ开始之LINQ to Objects(上)
    vue.js devtools-------调试vue.js的开发者插件
    Sublime快捷键(一)
    配置node,sass,淘宝镜像环境
    vue--------脚手架vue-cli搭建
    Sublime之插件的安装(一)
    计算机网络之万维网WWW
    官网下载java相关资源
  • 原文地址:https://www.cnblogs.com/YingLai/p/9289137.html
Copyright © 2020-2023  润新知