'; var adpushup = adpushup || {}; adpushup.que = adpushup.que || []; adpushup.que.push(function() { adpushup.triggerAd(ad_id); });
In the pandas series constructor, the method called dropna() is used to remove missing values from a series object. And it does not update the original series object with removed NaN values instead of updating the original series object, it will return another series object with updated values.
The parameters of the dropna() method are axis, inplace, and how.
Example 1
# importing packagesimport pandas as pdimport numpy as np# Creating Series objectssr = pd.Series([42, np.nan, 55, 42, np.nan, 73, np.nan, 55, 76, 87], index=list("ABCDEFGHIJ"))print('Series object:',sr)# Remove missing elementsresult = sr.dropna()# display outputprint(result)
Explanation
Initially, we have created a pandas Series with labeled index values and there are some Nan values present in the series object. After creating a pandas series object we have applied the dropna() method to remove missing values.
Output
Series object:A 42.0B NaNC 55.0D 42.0E NaNF 73.0G NaNH 55.0I 76.0J 87.0dtype: float64A 42.0C 55.0D 42.0F 73.0H 55.0I 76.0J 87.0dtype: float64
In the above output block, we can see both initial and resultant series objects. The second series object is the output object with removed missing values.
Example 2
# importing packagesimport pandas as pdimport numpy as npdates = pd.date_range('2021-06-01', periods=10, freq='D')#creating pandas Series with date indexsr = pd.Series([np.nan, 61, 72, 11, np.nan, 24, 56, 30, np.nan, 55], index=dates)print('Series object:',sr)# Remove missing elementsresult = sr.dropna()# display outputprint(result)
Explanation
In the following example, we have created a pandas Series with date range index values and there are some Nan values present in the series object “sr”. After creating a pandas series object we applied the dropna() method to remove those Nan values.
Output
Series object:2021-06-01 NaN2021-06-02 61.02021-06-03 72.02021-06-04 11.02021-06-05 NaN2021-06-06 24.02021-06-07 56.02021-06-08 30.02021-06-09 NaN2021-06-10 55.0Freq: D, dtype: float642021-06-02 61.02021-06-03 72.02021-06-04 11.02021-06-06 24.02021-06-07 56.02021-06-08 30.02021-06-10 55.0dtype: float64
Here we got a new series object with removed Nan values. In the above output block, we can see both initial and resultant series objects. The first object is the initial series, and the second one is the output of the dropna() method.
Updated on: 09-Mar-2022
5K+ Views
Related Articles
- How to leave nan as nan in the pandas series argsort method?
- How to remove a group of elements from a pandas series object?
- How does pandas series argsort handles nan values?
- How to remove a specified row From the Pandas Series Using Drop() method?
- How to Remove NaN Values from a Matrix using MATLAB
- How to replace NaN values by Zeroes in a column of a Pandas Series?
- How to check whether the Pandas series is having Nan values or not?
- How to remove rows in a Pandas series with duplicate indices?
- How to create a Pandas series from a python dictionary?
- How to create a series from a list using Pandas?
- How to get few rows from a Series in Pandas?
- Python Pandas - Return a Series containing counts of unique values from Index object considering NaN values as well
- How to remove rows from data frame in R that contains NaN?
- Creating a Dataframe from Pandas series
- How to sort a Pandas Series?
Advertisem*nts
';adpushup.triggerAd(ad_id); });