Skip to content

Commit 6549a74

Browse files
authored
DOC: fix doc to understand fillna with second dataframe (#43529)
* DOC: added missing value in column D for better understanding fillna with second dataframe * add note about column D
1 parent ecff20e commit 6549a74

File tree

1 file changed

+33
-31
lines changed

1 file changed

+33
-31
lines changed

pandas/core/generic.py

+33-31
Original file line numberDiff line numberDiff line change
@@ -6216,64 +6216,66 @@ def fillna(
62166216
--------
62176217
>>> df = pd.DataFrame([[np.nan, 2, np.nan, 0],
62186218
... [3, 4, np.nan, 1],
6219-
... [np.nan, np.nan, np.nan, 5],
6219+
... [np.nan, np.nan, np.nan, np.nan],
62206220
... [np.nan, 3, np.nan, 4]],
62216221
... columns=list("ABCD"))
62226222
>>> df
6223-
A B C D
6224-
0 NaN 2.0 NaN 0
6225-
1 3.0 4.0 NaN 1
6226-
2 NaN NaN NaN 5
6227-
3 NaN 3.0 NaN 4
6223+
A B C D
6224+
0 NaN 2.0 NaN 0.0
6225+
1 3.0 4.0 NaN 1.0
6226+
2 NaN NaN NaN NaN
6227+
3 NaN 3.0 NaN 4.0
62286228
62296229
Replace all NaN elements with 0s.
62306230
62316231
>>> df.fillna(0)
6232-
A B C D
6233-
0 0.0 2.0 0.0 0
6234-
1 3.0 4.0 0.0 1
6235-
2 0.0 0.0 0.0 5
6236-
3 0.0 3.0 0.0 4
6232+
A B C D
6233+
0 0.0 2.0 0.0 0.0
6234+
1 3.0 4.0 0.0 1.0
6235+
2 0.0 0.0 0.0 0.0
6236+
3 0.0 3.0 0.0 4.0
62376237
62386238
We can also propagate non-null values forward or backward.
62396239
62406240
>>> df.fillna(method="ffill")
6241-
A B C D
6242-
0 NaN 2.0 NaN 0
6243-
1 3.0 4.0 NaN 1
6244-
2 3.0 4.0 NaN 5
6245-
3 3.0 3.0 NaN 4
6241+
A B C D
6242+
0 NaN 2.0 NaN 0.0
6243+
1 3.0 4.0 NaN 1.0
6244+
2 3.0 4.0 NaN 1.0
6245+
3 3.0 3.0 NaN 4.0
62466246
62476247
Replace all NaN elements in column 'A', 'B', 'C', and 'D', with 0, 1,
62486248
2, and 3 respectively.
62496249
62506250
>>> values = {{"A": 0, "B": 1, "C": 2, "D": 3}}
62516251
>>> df.fillna(value=values)
6252-
A B C D
6253-
0 0.0 2.0 2.0 0
6254-
1 3.0 4.0 2.0 1
6255-
2 0.0 1.0 2.0 5
6256-
3 0.0 3.0 2.0 4
6252+
A B C D
6253+
0 0.0 2.0 2.0 0.0
6254+
1 3.0 4.0 2.0 1.0
6255+
2 0.0 1.0 2.0 3.0
6256+
3 0.0 3.0 2.0 4.0
62576257
62586258
Only replace the first NaN element.
62596259
62606260
>>> df.fillna(value=values, limit=1)
6261-
A B C D
6262-
0 0.0 2.0 2.0 0
6263-
1 3.0 4.0 NaN 1
6264-
2 NaN 1.0 NaN 5
6265-
3 NaN 3.0 NaN 4
6261+
A B C D
6262+
0 0.0 2.0 2.0 0.0
6263+
1 3.0 4.0 NaN 1.0
6264+
2 NaN 1.0 NaN 3.0
6265+
3 NaN 3.0 NaN 4.0
62666266
62676267
When filling using a DataFrame, replacement happens along
62686268
the same column names and same indices
62696269
62706270
>>> df2 = pd.DataFrame(np.zeros((4, 4)), columns=list("ABCE"))
62716271
>>> df.fillna(df2)
6272-
A B C D
6273-
0 0.0 2.0 0.0 0
6274-
1 3.0 4.0 0.0 1
6275-
2 0.0 0.0 0.0 5
6276-
3 0.0 3.0 0.0 4
6272+
A B C D
6273+
0 0.0 2.0 0.0 0.0
6274+
1 3.0 4.0 0.0 1.0
6275+
2 0.0 0.0 0.0 NaN
6276+
3 0.0 3.0 0.0 4.0
6277+
6278+
Note that column D is not affected since it is not present in df2.
62776279
"""
62786280
inplace = validate_bool_kwarg(inplace, "inplace")
62796281
value, method = validate_fillna_kwargs(value, method)

0 commit comments

Comments
 (0)