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.gt() is used to compare two series and return Boolean value for every respective element.
Syntax: Series.gt(other, level=None, fill_value=None, axis=0)
other: other series to be compared with
level: int or name of level in case of multi level
fill_value: Value to be replaced instead of NaN
axis: 0 or ‘index’ to apply method by rows and 1 or ‘columns’ to apply by columns.
Return type: Boolean series
Note: The results are returned on the basis of comparision caller series > other series.
To download the data set used in following example, click here.
In the following examples, the data frame used contains data of some NBA players. The image of data frame before any operations is attached below.
In this example, the Age column and Weight columns are copared using .gt() method. Since values in weight columns are very large as compared to Age column, hence the values are divided by 10 first. Before comparing, Null rows are removed using .dropna() method to avoid errors.
As shown in the output image, the new column has True wherever value in Age column is greater than Weight/10.
Example #2: Handling NaN values
In this example, two series are created using
pd.Series(). The series contains null value too and hence 5 is passed to fill_value parameter to replace null values by 5.
As it can be seen in output, NaN values were replaced by 5 and the comparison is performed after the replacement and new values are used for comparison.
0 True 1 True 2 False 3 True 4 True 5 False 6 False 7 True 8 False dtype: bool
- Python | pandas.map()
- Python | Pandas DataFrame.ix[ ]
- Python | Pandas Series.sem()
- Python | Pandas dataframe.min()
- Python | Pandas Series.min()
- Python | Pandas DatetimeIndex.second
- Python | Pandas dataframe.sem()
- Python | Pandas dataframe.std()
- Python | Pandas Timestamp.now
- Python | Pandas dataframe.sum()
- Python | Pandas dataframe.ne()
- Python | Pandas TimedeltaIndex.contains
- Python | Pandas.to_datetime()
- Python | Pandas dataframe.mul()
- Python | Pandas dataframe.take()
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.