zoukankan      html  css  js  c++  java
  • pandas的数据对齐

     
    import pandas as pd
    import numpy as np
    
     
     

    1、Series数据对齐

     
    ps1 = pd.Series(np.arange(4),index=['a','b','c','d'])
    ps2 = pd.Series(np.arange(5),index=['a','c','e','f','g'])
    
     
     
    ps1
    
    a    0
    b    1
    c    2
    d    3
    dtype: int32
     
    ps2
    
    a    0
    c    1
    e    2
    f    3
    g    4
    dtype: int32
     
    print(ps1+ps2)
    ps1.add(ps2,fill_value = 0)
    
    a    0.0
    b    NaN
    c    3.0
    d    NaN
    e    NaN
    f    NaN
    g    NaN
    dtype: float64
    
    a    0.0
    b    1.0
    c    3.0
    d    3.0
    e    2.0
    f    3.0
    g    4.0
    dtype: float64
     

    Datafram 数据对齐

     
    #Datafram 数据对齐
    pd1 = pd.DataFrame(np.arange(12).reshape(4,3),index=['a','b','c','d'],columns=['A','B','C'])
    pd2 = pd.DataFrame(np.arange(9).reshape(3,3),index=['a','d','f'],columns=['A','B','D'])
    
     
     
    pd1
    
      A B C
    a 0 1 2
    b 3 4 5
    c 6 7 8
    d 9 10 11
     
    pd2
    
      A B D
    a 0 1 2
    d 3 4 5
    f 6 7 8
     
    pd1+pd2
    
      A B C D
    a 0.0 2.0 NaN NaN
    b NaN NaN NaN NaN
    c NaN NaN NaN NaN
    d 12.0 14.0 NaN NaN
    f NaN NaN NaN NaN
     
    print('*******处理缺失值*******')
    pd1.add(pd2,fill_value = 0)
    
    *******处理缺失值*******
    
      A B C D
    a 0.0 2.0 2.0 2.0
    b 3.0 4.0 5.0 NaN
    c 6.0 7.0 8.0 NaN
    d 12.0 14.0 11.0 5.0
    f 6.0 7.0 NaN 8.0
     

    混合运算

     
    s3 = pd2.iloc[0] 
    s3
    
    A    0
    B    1
    D    2
    Name: a, dtype: int32
     
    pd2
    
    
      A B D
    a 0 1 2
    d 3 4 5
    f 6 7 8
     
    pd2-s3
    
    
      A B D
    a 0 0 0
    d 3 3 3
    f 6 6 6

    志同道合一起学习,欢迎加入QQ群:878749917
  • 相关阅读:
    DBLE快速开始
    RPM包直接安装MySQL
    初试DTLE-单向-聚合-分散复制
    使用 TiUP cluster 在单机上安装TiDB
    如何升级到MySQL8.0
    SpringBoot集成AOP操作日志
    IDEA常用插件
    JAVA中如何实现字符串的反转?
    mysql数据库忘记密码时如何修改密码
    二分查找算法
  • 原文地址:https://www.cnblogs.com/gujianjian/p/15324303.html
Copyright © 2011-2022 走看看