【问题标题】:Most efficient way to add additional rows up to a certain level that are NaN in value for Pandas Series将附加行添加到特定级别的最有效方法,对于 Pandas 系列,这些行的值是 NaN
【发布时间】:2021-05-20 20:57:00
【问题描述】:

我有一个系列如下:

这可以变化到任何长度。我想添加额外的行,直到用 NaN 填充的某个索引级别(在本例中为 240)。

最好的方法是什么?

【问题讨论】:

    标签: python pandas dataframe series


    【解决方案1】:

    您可以使用.reindex(),并将range(1, 236)的索引范围扩展到range(1, 241),如下所示:

    rr = rr.reindex(range(1, 241))
    

    结果:

    rr.tail(5)
    
    
    236   NaN
    237   NaN
    238   NaN
    239   NaN
    240   NaN
    dtype: float64
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-11-10
      • 2021-08-08
      • 2017-03-30
      • 2018-11-08
      • 2014-05-19
      • 2021-12-25
      • 2018-11-29
      • 2018-11-09
      相关资源
      最近更新 更多