Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric Python packages. Pandas is one of those packages and makes importing and analyzing data much easier.
Series.nonzero() is an argument less method. Just like it name says, rather returning non zero values from a series, it returns index of all non zero values. The returned series of indices can be passed to
iloc method and return all non zero values.
Return type: Array of indices
In this example, a Series is created from a Python List using Pandas
Series() method. The series also contains some zero values. After that
nonzero() method is called on series and the result is stored in result variable. The result series is then passed to
iloc() method to return all non zero values at that indices.
(array([0, 2, 3, 5, 6, 8, 9]), ) 0 1 2 12 3 1 5 4 6 22 8 3 9 9 dtype: int64
As shown in output, the index position of every non zero elements was returned and the values at that position were returned using
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
- Python | Index of Non-Zero elements in Python list
- Replace all the NaN values with Zero's in a column of a Pandas dataframe
- Get minimum values in rows or columns with their index position in Pandas-Dataframe
- Pandas - Get the elements of series that are not present in other series
- Add a Pandas series to another Pandas series
- Python | Check if all values in numpy are zero
- Python | Get first index values in tuple of strings
- Python | Pandas Index.values
- Python | Pandas Series.str.index()
- Python | Pandas Series.index
- Python | Pandas Series.values
- Python | Pandas series.cumprod() to find Cumulative product of a Series
- Python | Pandas Series.str.replace() to replace text in a series
- Python | Pandas Series.astype() to convert Data type of series
- Python | Pandas Series.cumsum() to find cumulative sum of a Series
- Python | Pandas series.cummax() to find Cumulative maximum of a series
- Python | Pandas Series.cummin() to find cumulative minimum of a series
- Python | Pandas Series.mad() to calculate Mean Absolute Deviation of a Series
- How to get the powers of an array values element-wise in Python-Pandas?
- How to get values of an NumPy array at certain index positions?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : Akanksha_Rai