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
- Python | Pandas Index.values
- Python | Pandas Series.values
- Python | Pandas Series.str.index()
- Python | Pandas Series.index
- 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.astype() to convert Data type of series
- Python | Pandas series.cumprod() to find Cumulative product of a Series
- Python | Pandas Series.cumsum() to find cumulative sum of a Series
- Python | Pandas Series.mad() to calculate Mean Absolute Deviation of a Series
- Python | Get first index values in tuple of strings
- Python | Initializing dictionary with list index-values
- Python | Initializing dictionary with list index values
- Python | Pandas Index.all()
- Python | Pandas Index.any()
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