Change language

Python | Pandas Series.lt ()

Pandas Series.lt() is used to compare two rows and return a boolean value for each matching item.

Syntax: Series.lt (other, level = None, fill_value = None, axis = 0)

Parameters:
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: results are returned based on a comparison of the calling series "other series.

To load the dataset used in the following example, click here.

In the following examples, the data frame being used contains data some NBA players. An image of the data frame before any operations is attached below. 

Example # 1:

This example compares the Age and Weight columns using the .lt () method. Since the values ​​in the weight columns are very large compared to the age column, therefore, the values ​​are divisible by 10 first. The .dropna () method removes null rows before comparison to avoid errors.

# pandas module import

import pandas as pd 

 
# create data frame

data = pd.read_csv ( " https://media.python.engineering/wp-content/uploads/nba. csv "

  
# deleting null z to avoid errors

data.dropna (inplace = True

 
# other series

other = data [ " Weight " ] / 10

 
# method call and return to new column

data [ "Age & lt; Weight " ] = data [ "Age" ]. lt (other)

 
# display
data

Output:
As shown in the output image, the new column is True, where the value in the Age column is less, than Weight / 10.

Example # 2: Handling values NaN

In this example, two series are created using pd.Series () . The series also contains a value of zero and therefore 10 is passed to the fill_value parameter to replace the zero values ​​with 10.

# import pandas module

import pandas as pd 

 
# numpy module import

import numpy as np

 
# create episode 1

series1 = pd.Series ([ 11 , 21 , 2 , 43 , 9 , 27 , np.nan, 110 , np.nan])

 
# creating series 2

series2 = pd.Series ([ 16 , np.nan, 2 , 23 , 5 , 40 , np.nan, 0 , 19 ])

 
# setting the replacement value to zero

na_replace = 10

 
# call and save the result

result = series1.lt (series2, fill_value < / code> = na_replace)

 
# display
result

Output:
As you can see from the results, the NaN values ​​have been replaced with 5 and the comparison is done after the replacement, and the new values ​​are used for comparison.

 0 True 1 False 2 False 3 False 4 False 5 True 6 False 7 False 8 True dtype: bool 

Shop

Gifts for programmers

Learn programming in R: courses

$FREE
Gifts for programmers

Best Python online courses for 2022

$FREE
Gifts for programmers

Best laptop for Fortnite

$399+
Gifts for programmers

Best laptop for Excel

$
Gifts for programmers

Best laptop for Solidworks

$399+
Gifts for programmers

Best laptop for Roblox

$399+
Gifts for programmers

Best computer for crypto mining

$499+
Gifts for programmers

Best laptop for Sims 4

$

Latest questions

PythonStackOverflow

Common xlabel/ylabel for matplotlib subplots

1947 answers

PythonStackOverflow

Check if one list is a subset of another in Python

1173 answers

PythonStackOverflow

How to specify multiple return types using type-hints

1002 answers

PythonStackOverflow

Printing words vertically in Python

909 answers

PythonStackOverflow

Python Extract words from a given string

798 answers

PythonStackOverflow

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

606 answers

PythonStackOverflow

Python os.path.join () method

384 answers

PythonStackOverflow

Flake8: Ignore specific warning for entire file

360 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

How to specify multiple return types using type-hints

Printing words vertically in Python

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically