當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Python Pandas Series.bfill()用法及代碼示例

Pandas 係列是帶有軸標簽的一維ndarray。標簽不必是唯一的,但必須是可哈希的類型。該對象同時支持基於整數和基於標簽的索引,並提供了許多方法來執行涉及索引的操作。

Pandas Series.bfill()函數是向後填充方法的同義詞。此函數用於填充給定係列對象中的缺失值。

用法: Series.bfill(axis=None, inplace=False, limit=None, downcast=None)

參數:
axis:軸= 1
inplace:對同一對象進行更改
limit:要填充的連續缺失值的最大數量

返回:係列

範例1:采用Series.bfill()函數來填充給定係列對象中的缺失值。

# importing pandas as pd 
import pandas as pd 
  
# Creating the Series 
sr = pd.Series(['New York', 'Chicago', 'Toronto', None, 'Rio']) 
  
# Create the Index 
index_ = ['City 1', 'City 2', 'City 3', 'City 4', 'City 5']  
  
# set the index 
sr.index = index_ 
  
# Print the series 
print(sr)

輸出:

現在我們將使用Series.bfill()函數來填充給定係列對象中的缺失值。

# fill the missing values using backward fill method 
result = sr.bfill() 
  
# Print the result 
print(result)

輸出:

正如我們在輸出中看到的,Series.bfill()函數已使用向後填充方法成功填充了給定係列對象中的缺失值。

範例2:采用Series.bfill()函數來填充給定係列對象中的缺失值。

# importing pandas as pd 
import pandas as pd 
  
# Creating the Series 
sr = pd.Series([100, None, None, 18, 65, None, 32, 10, 5, 24, None]) 
  
# Create the Index 
index_ = pd.date_range('2010-10-09', periods = 11, freq ='M') 
  
# set the index 
sr.index = index_ 
  
# Print the series 
print(sr)

輸出:

現在我們將使用Series.bfill()函數來填充給定係列對象中的缺失值。

# fill the missing values using backward fill method 
result = sr.bfill() 
  
# Print the result 
print(result)

輸出:

正如我們在輸出中看到的,Series.bfill()函數已使用向後填充方法成功填充了給定係列對象中的缺失值。請注意,最後一個值尚未填充,因為該元素之後的係列中沒有有效值。



相關用法


注:本文由純淨天空篩選整理自Shubham__Ranjan大神的英文原創作品 Python | Pandas Series.bfill()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。