Skip to content

Commit 94c6c0c

Browse files
JennaVergeynstjorisvandenbossche
authored andcommitted
DOC: add examples to DataFrame.dropna (#15620)
1 parent a37c610 commit 94c6c0c

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

pandas/core/frame.py

+44
Original file line numberDiff line numberDiff line change
@@ -3084,6 +3084,50 @@ def dropna(self, axis=0, how='any', thresh=None, subset=None,
30843084
Returns
30853085
-------
30863086
dropped : DataFrame
3087+
3088+
Examples
3089+
--------
3090+
>>> df = pd.DataFrame([[np.nan, 2, np.nan, 0], [3, 4, np.nan, 1],
3091+
... [np.nan, np.nan, np.nan, 5]],
3092+
... columns=list('ABCD'))
3093+
>>> df
3094+
A B C D
3095+
0 NaN 2.0 NaN 0
3096+
1 3.0 4.0 NaN 1
3097+
2 NaN NaN NaN 5
3098+
3099+
Drop the columns where all elements are nan:
3100+
3101+
>>> df.dropna(axis=1, how='all')
3102+
A B D
3103+
0 NaN 2.0 0
3104+
1 3.0 4.0 1
3105+
2 NaN NaN 5
3106+
3107+
Drop the columns where any of the elements is nan
3108+
3109+
>>> df.dropna(axis=1, how='any')
3110+
D
3111+
0 0
3112+
1 1
3113+
2 5
3114+
3115+
Drop the rows where all of the elements are nan
3116+
(there is no row to drop, so df stays the same):
3117+
3118+
>>> df.dropna(axis=0, how='all')
3119+
A B C D
3120+
0 NaN 2.0 NaN 0
3121+
1 3.0 4.0 NaN 1
3122+
2 NaN NaN NaN 5
3123+
3124+
Keep only the rows with at least 2 non-na values:
3125+
3126+
>>> df.dropna(thresh=2)
3127+
A B C D
3128+
0 NaN 2.0 NaN 0
3129+
1 3.0 4.0 NaN 1
3130+
30873131
"""
30883132
inplace = validate_bool_kwarg(inplace, 'inplace')
30893133
if isinstance(axis, (tuple, list)):

0 commit comments

Comments
 (0)