zoukankan      html  css  js  c++  java
  • Pandas入门之六:重建索引

    已信任
    Jupyter 服务器: 本地
    Python 3: Not Started
    [11]
    
    
    
    import pandas as pd
    import numpy as np
    
    
    
    [13]
    
    
    
    
    df = pd.DataFrame({
        'a':pd.date_range(start='2021-07-14', periods=5, freq='D'),
        'b':[1,2,3,4,5],
        'c':[0.1,0.2,0.3,0.4,0.5]
    })
    df
    a    b    c
    0    2021-07-14    1    0.1
    1    2021-07-15    2    0.2
    2    2021-07-16    3    0.3
    3    2021-07-17    4    0.4
    4    2021-07-18    5    0.5
    [14]
    
    
    
    # 重建索引reindex
    df.reindex(index=[0,2,4], columns=['a','b','d'])
    a    b    d
    0    2021-07-14    1    NaN
    2    2021-07-16    3    NaN
    4    2021-07-18    5    NaN
    [16]
    
    
    
    df.reindex(index=[0,5,6], columns=['a','b','c'],method='ffill')# 向前填充,5向前是4,所以为 5 0.5
    a    b    c
    0    2021-07-14    1    0.1
    5    2021-07-18    5    0.5
    6    2021-07-18    5    0.5
    [18]
    
    
    
    df1 = pd.DataFrame({
        'g':pd.date_range(start='2021-07-14', periods=5, freq='D'),
        'b':[1,2,3,4,5],
        'c':[0.1,0.2,0.3,0.4,0.5]
    })
    df1
    g    b    c
    0    2021-07-14    1    0.1
    1    2021-07-15    2    0.2
    2    2021-07-16    3    0.3
    3    2021-07-17    4    0.4
    4    2021-07-18    5    0.5
    [19]
    
    
    
    # 将df索引修改为像df1的索引
    df.reindex_like(df1)
    g    b    c
    0    NaN    1    0.1
    1    NaN    2    0.2
    2    NaN    3    0.3
    3    NaN    4    0.4
    4    NaN    5    0.5
    [20]
    
    
    
    df1
    g    b    c
    0    2021-07-14    1    0.1
    1    2021-07-15    2    0.2
    2    2021-07-16    3    0.3
    3    2021-07-17    4    0.4
    4    2021-07-18    5    0.5
    [22]
    
    
    
    # 重命名索引
    df1.rename(columns=({'g':'f','b':'hello','c':'world'}))
    f    hello    world
    0    2021-07-14    1    0.1
    1    2021-07-15    2    0.2
    2    2021-07-16    3    0.3
    3    2021-07-17    4    0.4
    4    2021-07-18    5    0.5
    [-]
  • 相关阅读:
    opensuse tumbleweed中安装code
    树莓派中将caplock映射为esc键
    记录一次奇怪但是很有意义的程序编译警告
    新树莓派系统安装ROS记录
    程序的深挖
    intle官方手册下载
    slax linux的定制
    angular4 *ngFor获取index
    axios post传参后台无法接收问题
    AMD、CMD、CommonJs和 ES6对比
  • 原文地址:https://www.cnblogs.com/vvzhang/p/15009064.html
Copyright © 2011-2022 走看看