Articles

pandas DataFrame Comparision method - DataFrame.le()

pandas DataFrame Comparision method - DataFrame.le()


In this pandas tutorial we will discuss about pandas le() comparison method.

 

Introduction

DataFrame is an two dimensional data structure that will store data in two dimensional format. One dimension refers to a row and second dimension refers to a column, So It will store the data in rows and columns.

We can create this DataFrame using DataFrame() method. But this is available in pandas module, so we have to import pandas module.

Syntax:

pandas.DataFrame(data)

Where, data is the input dataframe , The data can be a dictionary that stores list of values with specified key.

 

ExampleCreate Pandas dataframe

In this example, we will create a dataframe with 4 rows and 4 columns with integers data and assign indices through index parameter.

import pandas as pd

#create dataframe from the integers data
data= pd.DataFrame({'column1':[100,200,300,200],

                    'column2':[100,200,300,200],

                   "column3":[23,45,67,43],

                    "column4":[1234,54,67,8]

                   },index=['one','two','three','four'])

#display the dataframe
print(data)

Output: DataFrame is created below

       column1  column2  column3  column4
one        100      100       23     1234
two        200      200       45       54
three      300      300       67       67
four       200      200       43        8

DataFrame.le() - less than or equal to

This method is used to check the given value is less than or equal to to the elements present in the dataframe or not.

If present, It will return True at that element position, otherwise it will return false.

Syntax:

dataframe_input.le(value)

where, dataframe_input is the input pandas dataframe.

 

Example:

In this example, we will create a dataframe and check the le() method functionality with two scenarios.

import pandas as pd

#create dataframe from the integers data
data= pd.DataFrame({'column1':[100,200,300,200],

                    'column2':[100,200,300,200],

                   "column3":[23,45,67,43],

                    "column4":[1234,54,67,8]

                   },index=['one','two','three','four'])

#check the elements present in the dataframe are less than or equal to  200 or not
print(data.le(200))

print()

#check the elements present in the dataframe are less than or equal to 23 or not
print(data.le(23))

Output:

In the first output, the elements that are less than or equal to  200 will be replaced by True, others are set to False.

 

Similarly, in the second output, the elements that are less than or equal to  23 will be replaced by True, others are set to False.

       column1  column2  column3  column4
one       True     True     True    False
two       True     True     True     True
three    False    False     True     True
four      True     True     True     True

       column1  column2  column3  column4
one      False    False     True    False
two      False    False    False    False
three    False    False    False    False
four     False    False    False     True

We can also place an operator instead as le(). The operator used is "<=".

Syntax:

dataframe_input<=value

Example:

In this example, we will create a dataframe and check the "<=" operator  functionality with two scenarios.

import pandas as pd

#create dataframe from the integers data
data= pd.DataFrame({'column1':[100,200,300,200],

                    'column2':[100,200,300,200],

                   "column3":[23,45,67,43],

                    "column4":[1234,54,67,8]

                   },index=['one','two','three','four'])

#check the elements present in the dataframe are less than or equal to 200 or not
print(data<=200)

print()

#check the elements present in the dataframe are less than or equal to  23 or not
print(data<=23)

Output:

In the first output, the elements that are less than or equal to 200 will be replaced by True, others are set to False.

 

Similarly, in the second output, the elements that are less than or equal to 23 will be replaced by True, others are set to False.

       column1  column2  column3  column4
one       True     True     True    False
two       True     True     True     True
three    False    False     True     True
four      True     True     True     True

       column1  column2  column3  column4
one      False    False     True    False
two      False    False    False    False
three    False    False    False    False
four     False    False    False     True

We can also compare with column wise by providing different values.

Note - The total values must be equal to column count

Syntax:

dataframe_input.le([values])

Example:

In this example, we will compare the dataframe with 4 values (since number of columns are 4)

import pandas as pd

#create dataframe from the integers data
data= pd.DataFrame({'column1':[100,200,300,200],

                    'column2':[100,200,300,200],

                   "column3":[23,45,67,43],

                    "column4":[1234,54,67,8]

                   },index=['one','two','three','four'])

#display actual dataframe
print(data)

print()

#check the elements present in the dataframe are less than or equal to to the given values in list or not
print(data.le([100, 200, 23,8]))

Output:

From the above example, the first value - 100 is compared with first column and set to True whereever 100 is less than or equal to the element present in the first column,the second value - 200 is compared with second column and set to True whereever 200 is less than or equal to the element present in the second column,the third value - 23 is compared with third column and set to True whereever 23is less than or equal to the element present in the third column and the forth value - 8 is compared with forth column and set to True whereever 8 is less than or equal to the element present in the forth column.  Remaining all are set to False.

       column1  column2  column3  column4
one        100      100       23     1234
two        200      200       45       54
three      300      300       67       67
four       200      200       43        8

       column1  column2  column3  column4
one       True     True     True    False
two      False     True    False    False
three    False    False    False    False
four     False     True    False     True

 


Pandas

Would you like to see your article here on tutorialsinhand. Join Write4Us program by tutorialsinhand.com

About the Author
Gottumukkala Sravan Kumar 171FA07058
B.Tech (Hon's) - IT from Vignan's University. Published 1400+ Technical Articles on Python, R, Swift, Java, C#, LISP, PHP - MySQL and Machine Learning
Page Views :    Published Date : Apr 29,2023  
Please Share this page

Related Articles

Like every other website we use cookies. By using our site you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service. Learn more Got it!