zoukankan      html  css  js  c++  java
  • DataFrame合并:合并重叠数据combine_first

    from pandas import DataFrame,Series
    import numpy as np
    
    a = Series([np.nan,2.5,np.nan,3.5,4.5,np.nan],
               index=['f','e','d','c','b','a'])
    print(a)
    '''
    f    NaN
    e    2.5
    d    NaN
    c    3.5
    b    4.5
    a    NaN
    dtype: float64
    '''
    b = Series(np.arange(len(a),dtype=np.float64),
               index=['f','e','d','c','b','a'])
    print(b)
    '''
    f    0.0
    e    1.0
    d    2.0
    c    3.0
    b    4.0
    a    5.0
    dtype: float64
    '''
    print(b[:-2])
    '''
    f    0.0
    e    1.0
    d    2.0
    c    3.0
    dtype: float64
    '''
    print(a[2:])
    '''
    d    NaN
    c    3.5
    b    4.5
    a    NaN
    dtype: float64
    '''
    # 索引全部或部分重叠的两个数据集
    # combine_first 用参数对象的数据未调用者对象的缺失数据“打补丁”
    print(b[:-2].combine_first(a[2:]))
    '''
    a    NaN
    b    4.5
    c    3.0
    d    2.0
    e    1.0
    f    0.0
    dtype: float64
    '''
    
    df1 = DataFrame({'a':[1.0,np.nan,5.0,np.nan],
                     'b':[np.nan,2.0,np.nan,6.0],
                     'c': range(2,18,4)})
    print(df1)
    '''
         a    b   c
    0  1.0  NaN   2
    1  NaN  2.0   6
    2  5.0  NaN  10
    3  NaN  6.0  14
    '''
    df2 = DataFrame({'a':[5.0,4.0,np.nan,3.0,7.0],
                     'b':[np.nan,3,4,6,8]})
    print(df2)
    '''
         a    b
    0  5.0  NaN
    1  4.0  3.0
    2  NaN  4.0
    3  3.0  6.0
    4  7.0  8.0
    '''
    print(df1.combine_first(df2))
    '''
         a    b     c
    0  1.0  NaN   2.0
    1  4.0  2.0   6.0
    2  5.0  4.0  10.0
    3  3.0  6.0  14.0
    4  7.0  8.0   NaN
    '''
  • 相关阅读:
    Ansible概述
    iptables端口转发
    iptables配置实例
    iptables常用操作
    iptables常用命令
    每日总结3.15
    每日总结3.12
    每日总结3.11
    每日总结3.10
    每日总结3.9
  • 原文地址:https://www.cnblogs.com/nicole-zhang/p/14536281.html
Copyright © 2011-2022 走看看