Home »
Python »
Python Programs
Python - USING LIKE inside pandas query
Learn, how to use / implement USING LIKE inside Python pandas query?
By Pranit Sharma Last updated : September 27, 2023
Pandas is a special tool that allows us to perform complex manipulations of data effectively and efficiently. Inside pandas, we mostly deal with a dataset in the form of DataFrame. DataFrames are 2-dimensional data structures in pandas. DataFrames consist of rows, columns, and data.
A database is an organized and structured collection of data stored in a computer system or any cloud storage. Small databases can be stored on a file system. Database management operations are done in SQL language which is a structured query language.
This structured query language has several inbuilt keywords, for example, select, where, 'Like', etc.
LIKE Keyword
The LIKE keyword is used as a condition where if a particular value is similar to some condition, it returns that value.
USING LIKE inside pandas query
There is a myth that pandas DataFrame query also contains the Like keyword which is exactly like SQL. This is not true, the pandas dataframe.query() method contains a string where we write a certain condition on the basis of which we get the results.
Let us understand with the help of an example,
Python program to demonstrate USING LIKE inside pandas query
# Importing pandas package
import pandas as pd
# Creating dictionary
d = {
'Name':['Ayush','Ashutosh','Bobby','Bhavya','Chetan','Chinki','Dhruv','Esha'],
'City':['Surat','Vadodra','Chandigarh','Ahemdabad','Bhopal','Delhi','Mumbai','Mumbai']
}
# Creating DataFrame
df = pd.DataFrame(d)
# Display Original DataFrames
print("Created DataFrame:\n",df,"\n")
# Display columns with using query
print(df.query('Name.str.startswith("Ch")', engine='python'))
Output
The output of the above program is:
Python Pandas Programs »