Method for filling missing data in Fillna of Pandas

  • 2021-07-01 07:47:57
  • OfStack

Agreement:


import pandas as pd
import numpy as np
from numpy import nan as NaN

Fill in missing data

fillna () is the main treatment.


df1=pd.DataFrame([[1,2,3],[NaN,NaN,2],[NaN,NaN,NaN],[8,8,NaN]])
df1

Code result:

0 1 2
0 1.0 2.0 3.0
1 NaN NaN 2.0
2 NaN NaN NaN
3 8.0 8.0 NaN

To fill with constants:


df1.fillna(100)

Code result:

0 1 2
0 1.0 2.0 3.0
1 100.0 100.0 2.0
2 100.0 100.0 100.0
3 8.0 8.0 100.0

Fill in different constants through dictionaries:


df1.fillna({0:10,1:20,2:30})

Code result:

0 1 2
0 1.0 2.0 3.0
1 10.0 20.0 2.0
2 10.0 20.0 30.0
3 8.0 8.0 30.0

Pass in inplace=True to directly modify the original object:


df1.fillna(0,inplace=True)
df1

Code result:

0 1 2
0 1.0 2.0 3.0
1 0.0 0.0 2.0
2 0.0 0.0 0.0
3 8.0 8.0 0.0

Pass in method= "" to change the interpolation mode:


df2=pd.DataFrame(np.random.randint(0,10,(5,5)))
df2.iloc[1:4,3]=NaN;df2.iloc[2:4,4]=NaN
df2

Code result:

0 1 2 3 4
0 6 6 2 4.0 1.0
1 4 7 0 NaN 5.0
2 6 5 5 NaN NaN
3 1 9 9 NaN NaN
4 4 8 1 5.0 9.0


df2.fillna(method='ffill')# Fill with the previous value 

Code result:

0 1 2 3 4
0 6 6 2 4.0 1.0
1 4 7 0 4.0 5.0
2 6 5 5 4.0 5.0
3 1 9 9 4.0 5.0
4 4 8 1 5.0 9.0

Passing in limit= "" Limit the number of padding:


df2.fillna(method='bfill',limit=2)

Code result:

0 1 2 3 4
0 6 6 2 4.0 1.0
1 4 7 0 NaN 5.0
2 6 5 5 5.0 9.0
3 1 9 9 5.0 9.0
4 4 8 1 5.0 9.0

Pass in axis= "" to modify the fill direction:


df2.fillna(method="ffill",limit=1,axis=1)

Code result:

0 1 2 3 4
0 6.0 6.0 2.0 4.0 1.0
1 4.0 7.0 0.0 0.0 5.0
2 6.0 5.0 5.0 5.0 NaN
3 1.0 9.0 9.0 9.0 NaN
4 4.0 8.0 1.0 5.0 9.0


Related articles: