Movatterモバイル変換


[0]ホーム

URL:


Skip to main content
Ctrl+K

pandas.DataFrame.dropna#

DataFrame.dropna(*,axis=0,how=<no_default>,thresh=<no_default>,subset=None,inplace=False,ignore_index=False)[source]#

Remove missing values.

See theUser Guide for more on which values areconsidered missing, and how to work with missing data.

Parameters:
axis{0 or ‘index’, 1 or ‘columns’}, default 0

Determine if rows or columns which contain missing values areremoved.

  • 0, or ‘index’ : Drop rows which contain missing values.

  • 1, or ‘columns’ : Drop columns which contain missing value.

Only a single axis is allowed.

how{‘any’, ‘all’}, default ‘any’

Determine if row or column is removed from DataFrame, when we haveat least one NA or all NA.

  • ‘any’ : If any NA values are present, drop that row or column.

  • ‘all’ : If all values are NA, drop that row or column.

threshint, optional

Require that many non-NA values. Cannot be combined with how.

subsetcolumn label or sequence of labels, optional

Labels along other axis to consider, e.g. if you are dropping rowsthese would be a list of columns to include.

inplacebool, default False

Whether to modify the DataFrame rather than creating a new one.

ignore_indexbool, defaultFalse

IfTrue, the resulting axis will be labeled 0, 1, …, n - 1.

Added in version 2.0.0.

Returns:
DataFrame or None

DataFrame with NA entries dropped from it or None ifinplace=True.

See also

DataFrame.isna

Indicate missing values.

DataFrame.notna

Indicate existing (non-missing) values.

DataFrame.fillna

Replace missing values.

Series.dropna

Drop missing values.

Index.dropna

Drop missing indices.

Examples

>>>df=pd.DataFrame({"name":['Alfred','Batman','Catwoman'],..."toy":[np.nan,'Batmobile','Bullwhip'],..."born":[pd.NaT,pd.Timestamp("1940-04-25"),...pd.NaT]})>>>df       name        toy       born0    Alfred        NaN        NaT1    Batman  Batmobile 1940-04-252  Catwoman   Bullwhip        NaT

Drop the rows where at least one element is missing.

>>>df.dropna()     name        toy       born1  Batman  Batmobile 1940-04-25

Drop the columns where at least one element is missing.

>>>df.dropna(axis='columns')       name0    Alfred1    Batman2  Catwoman

Drop the rows where all elements are missing.

>>>df.dropna(how='all')       name        toy       born0    Alfred        NaN        NaT1    Batman  Batmobile 1940-04-252  Catwoman   Bullwhip        NaT

Keep only the rows with at least 2 non-NA values.

>>>df.dropna(thresh=2)       name        toy       born1    Batman  Batmobile 1940-04-252  Catwoman   Bullwhip        NaT

Define in which columns to look for missing values.

>>>df.dropna(subset=['name','toy'])       name        toy       born1    Batman  Batmobile 1940-04-252  Catwoman   Bullwhip        NaT

[8]ページ先頭

©2009-2025 Movatter.jp