Pandas Series: dropna() function
Analyze and drop Rows/Columns with Null values in a Pandas series
The dropna() function is used to return a new Series with missing values removed.
Syntax:
Series.dropna(self, axis=0, inplace=False, **kwargs)
Parameters:
Name | Description | Type/Default Value | Required / Optional |
---|---|---|---|
axis | There is only one axis to drop values from. | {0 or ‘index’} Default Value: 0 |
Required |
inplace | If True, do operation inplace and return None. | bool Default Value: False |
Required |
inplace | Whether to perform the operation in place on the data. | bool Default Value: False |
Required |
**kwargs | Not in use. | Required |
Returns: Series- Series with NA entries dropped from it.
Example:
Python-Pandas Code:
import numpy as np
import pandas as pd
s = pd.Series([2., 3., np.nan])
s
Output:
0 2.0 1 3.0 2 NaN dtype: float64
Example - Drop NA values from a Series:
Python-Pandas Code:
import numpy as np
import pandas as pd
s = pd.Series([2., 3., np.nan])
s.dropna()
Output:
0 2.0 1 3.0 dtype: float64
Example - Keep the Series with valid entries in the same variable:
Python-Pandas Code:
import numpy as np
import pandas as pd
s = pd.Series([2., 3., np.nan])
s.dropna(inplace=True)
s
Output:
0 2.0 1 3.0 dtype: float64
Example - Empty strings are not considered NA values. None is considered an NA value:
Python-Pandas Code:
import numpy as np
import pandas as pd
s = pd.Series([2., 3., np.nan])
s = pd.Series([np.NaN, 2, pd.NaT, '', None, 'I am'])
s
Output:
0 NaN 1 2 2 NaT 3 4 None 5 I am dtype: object
Python-Pandas Code:
import numpy as np
import pandas as pd
s = pd.Series([2., 3., np.nan])
s = pd.Series([np.NaN, 2, pd.NaT, '', None, 'I am'])
s.dropna()
Output:
1 2 3 5 I am dtype: object
Previous: Detect existing values in Pandas series
Next: Fill NA/NaN values using the specified method
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics