Pandas 係列是帶有軸標簽的一維 ndarray。標簽不必是唯一的,但必須是可散列的類型。該對象支持基於整數和基於標簽的索引,並提供了許多用於執行涉及索引的操作的方法。
Pandas Series.fillna()
函數用於使用指定的方法填充 NA/NaN 值。
用法: Series.fillna(value=None, method=None, axis=None, inplace=False, limit=None, downcast=None, **kwargs)
參數:
value:用於填充孔的值
method:用於在重新索引的 Series pad/ffill 中填充孔的方法
axis:{0 或 ‘index’}
inplace:如果為真,請填寫到位。
limit:如果指定了方法,這是向前/向後填充的連續 NaN 值的最大數量
downcast:字典,默認為無
返回:填充:係列
範例1:使用Series.fillna()
函數來填寫給定係列對象中的缺失值。使用字典傳遞對應於係列對象中不同索引標簽的要填充的值。
# importing pandas as pd
import pandas as pd
# Creating the Series
sr = pd.Series(['New York', 'Chicago', 'Toronto', None, 'Rio'])
# Create the Index
sr.index = ['City 1', 'City 2', 'City 3', 'City 4', 'City 5']
# set the index
sr.index = index_
# Print the series
print(sr)
輸出:
現在我們將使用Series.fillna()
函數來填寫給定係列對象中的缺失值。
# fill the values using dictionary
result = sr.fillna(value = {'City 4' :'Lisbon', 'City 1' :'Dublin'})
# Print the result
print(result)
輸出:
正如我們在輸出中看到的,Series.fillna()
函數已成功填寫給定係列對象中的缺失值。
範例2:使用Series.fillna()
函數使用前向填充 (ffill) 方法填充給定係列對象中的缺失值。
# 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.fillna()
函數來填寫給定係列對象中的缺失值。我們將使用前向填充方法來填充缺失值。
# fill the values using forward fill method
result = sr.fillna(method = 'ffill')
# Print the result
print(result)
輸出:
正如我們在輸出中看到的,Series.fillna()
函數已成功填寫給定係列對象中的缺失值。
相關用法
- Python pandas.to_markdown()用法及代碼示例
- Python Pandas Index.insert()用法及代碼示例
- Python Pandas DatetimeIndex.inferred_freq用法及代碼示例
- Python Pandas PeriodIndex.start_time用法及代碼示例
- Python Pandas PeriodIndex.week用法及代碼示例
- Python Pandas Timestamp.second用法及代碼示例
- Python Pandas Series.asobject用法及代碼示例
- Python Pandas DataFrame.reset_index()用法及代碼示例
- Python Pandas dataframe.notna()用法及代碼示例
- Python Pandas PeriodIndex.weekday用法及代碼示例
- Python Pandas Series.dt.floor用法及代碼示例
- Python Pandas Index.get_slice_bound()用法及代碼示例
- Python Pandas Dataframe.duplicated()用法及代碼示例
- Python Pandas dataframe.notnull()用法及代碼示例
- Python Pandas series.cumprod()用法及代碼示例
- Python Pandas Timestamp.date用法及代碼示例
- Python Pandas Timestamp.ctime用法及代碼示例
- Python Pandas dataframe.round()用法及代碼示例
- Python Pandas Series.searchsorted()用法及代碼示例
- Python pandas.date_range()用法及代碼示例
注:本文由純淨天空篩選整理自Shubham__Ranjan大神的英文原創作品 Python | Pandas Series.fillna()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。