zoukankan      html  css  js  c++  java
  • pandas.DataFrame.reindex_like的使用说明

    参考链接:https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.reindex_like.html#pandas.DataFrame.reindex_like

    pandas.DataFrame.reindex_like

    DataFrame.reindex_like(othermethod=Nonecopy=Truelimit=Nonetolerance=None)[source]

    Return an object with matching indices as other object.

    Conform the object to the same index on all axes. Optional filling logic, placing NaN in locations having no value in the previous index. A new object is produced unless the new index is equivalent to the current one and copy=False.

    Parameters
    otherObject of the same data type

    Its row and column indices are used to define the new indices of this object.

    method{None, ‘backfill’/’bfill’, ‘pad’/’ffill’, ‘nearest’}

    Method to use for filling holes in reindexed DataFrame. Please note: this is only applicable to DataFrames/Series with a monotonically increasing/decreasing index.

    • None (default): don’t fill gaps

    • pad / ffill: propagate last valid observation forward to next valid

    • backfill / bfill: use next valid observation to fill gap

    • nearest: use nearest valid observations to fill gap.

    copybool, default True

    Return a new object, even if the passed indexes are the same.

    limitint, default None

    Maximum number of consecutive labels to fill for inexact matches.

    toleranceoptional

    Maximum distance between original and new labels for inexact matches. The values of the index at the matching locations must satisfy the equation abs(index[indexer] target) <= tolerance.

    Tolerance may be a scalar value, which applies the same tolerance to all values, or list-like, which applies variable tolerance per element. List-like includes list, tuple, array, Series, and must be the same size as the index and its dtype must exactly match the index’s type.

    Returns
    Series or DataFrame

    Same type as caller, but with changed indices on each axis.

    Same as calling .reindex(index=other.index, columns=other.columns,...).

    跟这个效果一样reindex(index=other.index, columns=other.columns,...).

    这个是今天学习的4个方法中最简单的一个,其实就是去学习另外一个的df对象的index与columns。直接抄书中说明。

    In [146]: df1                                                                                               
    Out[146]: 
                temp_celsius  temp_fahrenheit windspeed
    2014-02-12          24.3             75.7      high
    2014-02-13          31.0             87.8      high
    2014-02-14          22.0             71.6    medium
    2014-02-15          35.0             95.0    medium
    
    In [147]: df2                                                                                               
    Out[147]: 
                temp_celsius windspeed
    2014-02-12          28.0       low
    2014-02-13          30.0       low
    2014-02-15          35.1    medium
    
    In [148]: df2.reindex_like(df1)                                                                             
    Out[148]: 
                temp_celsius  temp_fahrenheit windspeed
    2014-02-12          28.0              NaN       low
    2014-02-13          30.0              NaN       low
    2014-02-14           NaN              NaN       NaN
    2014-02-15          35.1              NaN    medium
    
    In [149]:     
    

      从代码中,可以清晰的看到df2,完全使用了df1的index与columns信息,并且在缺省的信息下,使用了NaN数据。

  • 相关阅读:
    移动端屏幕旋转的事件和样式方案。
    active:移动端触摸按钮的效果。
    移动端字体单位该使用px还是rem?
    Cordova/Ionic Android 开发环境搭建
    JavaScript 深拷贝(deep copy)和浅拷贝(shallow copy)
    你不知道的JS之 this 和对象原型(一)this 是什么
    你不知道的JS之作用域和闭包 附录
    你不知道的JS之作用域和闭包(五)作用域闭包
    你不知道的JS之作用域和闭包(四)(声明)提升
    你不知道的JS之作用域和闭包(三)函数 vs. 块级作用域
  • 原文地址:https://www.cnblogs.com/sidianok/p/14367400.html
Copyright © 2011-2022 走看看