DataFrame.fillna函数参数说明
DataFrame.fillna(value=None, method=None, axis=None, inplace=False, limit=None, downcast=None)
value :用来填充缺失值的值
inplace 参数的取值:True、False
True:直接修改原对象
False:创建一个副本,修改副本,原对象不变(缺省默认)
method 参数的取值: {‘backfill’, ‘bfill’, ‘pad’, ‘ffill’, None}, default None method = ‘ffill’/‘pad’:用前一个非缺失值去填充该缺失值; method = ‘bfill’/‘backfill’:用下一个非缺失值填充该缺失值;
axis 参数的取值: {0 or ‘index’, 1 or ‘columns’} 沿其填充缺失值的轴。
limit 参数:限制填充个数
import pandas as pd
import numpy as np
from numpy import nan
操作实例
demo = pd.DataFrame(np.random.randint(1,10,25).reshape(5,5))
n = 0
while(n<5):
demo.iloc[np.random.randint(0,5),np.random.randint(0,5)] = nan
n = n + 1
demo
| 0 | 1 | 2 | 3 | 4 |
---|
0 | 4 | 9 | 8.0 | 8.0 | 5.0 |
---|
1 | 2 | 9 | 3.0 | 3.0 | NaN |
---|
2 | 7 | 5 | NaN | 1.0 | 8.0 |
---|
3 | 1 | 3 | 5.0 | 7.0 | 1.0 |
---|
4 | 4 | 7 | NaN | NaN | 7.0 |
---|
用常数填充
demo.fillna(20.0)
| 0 | 1 | 2 | 3 | 4 |
---|
0 | 4 | 9 | 8.0 | 8.0 | 5.0 |
---|
1 | 2 | 9 | 3.0 | 3.0 | 20.0 |
---|
2 | 7 | 5 | 20.0 | 1.0 | 8.0 |
---|
3 | 1 | 3 | 5.0 | 7.0 | 1.0 |
---|
4 | 4 | 7 | 20.0 | 20.0 | 7.0 |
---|
inplace参数
hint:默认为False,创建副本。 设置为True时,在原表格中操作
demo1 = demo.copy()
print(demo1.fillna(30.0,inplace=True))
print(demo1)
None
0 1 2 3 4
0 4 9 8.0 8.0 5.0
1 2 9 3.0 3.0 30.0
2 7 5 30.0 1.0 8.0
3 1 3 5.0 7.0 1.0
4 4 7 30.0 30.0 7.0
method参数
hint:
method = 'ffill'/'pad' :用前一个非缺失值去填充该缺失值;
method = 'bfill'/'backfill' :用下一个非缺失值填充该缺失值;
demo.fillna(method='ffill')
| 0 | 1 | 2 | 3 | 4 |
---|
0 | 4 | 9 | 8.0 | 8.0 | 5.0 |
---|
1 | 2 | 9 | 3.0 | 3.0 | 5.0 |
---|
2 | 7 | 5 | 3.0 | 1.0 | 8.0 |
---|
3 | 1 | 3 | 5.0 | 7.0 | 1.0 |
---|
4 | 4 | 7 | 5.0 | 7.0 | 7.0 |
---|
demo.fillna(method='bfill')
| 0 | 1 | 2 | 3 | 4 |
---|
0 | 4 | 9 | 8.0 | 8.0 | 5.0 |
---|
1 | 2 | 9 | 3.0 | 3.0 | 8.0 |
---|
2 | 7 | 5 | 5.0 | 1.0 | 8.0 |
---|
3 | 1 | 3 | 5.0 | 7.0 | 1.0 |
---|
4 | 4 | 7 | NaN | NaN | 7.0 |
---|
指定axis参数
指定对行操作/对列操作
代码:
https://download.csdn.net/download/pylittlebrat/85600907?spm=1001.2014.3001.5501
|