Searching for a Word in Excel using Python Pandas
To search for a word in an Excel file using Python Pandas, you can follow the steps below:
- Import the required libraries:
- Read the Excel file into a Pandas DataFrame:
- Create a Boolean mask for the desired word:
- Filter the DataFrame using the Boolean mask:
- View the search results:
import pandas as pd
excel_file = 'path/to/excel/file.xlsx'
df = pd.read_excel(excel_file)
word = 'search_word'
mask = df.apply(lambda row: row.astype(str).str.contains(word, case=False).any(), axis=1)
search_results = df[mask]
print(search_results)
Here’s an example:
import pandas as pd
# Read the Excel file
excel_file = 'data.xlsx'
df = pd.read_excel(excel_file)
# Word to search for
word = 'apple'
# Create a Boolean mask
mask = df.apply(lambda row: row.astype(str).str.contains(word, case=False).any(), axis=1)
# Filter the DataFrame
search_results = df[mask]
# View the search results
print(search_results)
In this example, the code reads an Excel file named “data.xlsx” using Pandas. It then searches for the word “apple” in all the columns and returns the rows of the DataFrame that contain the word.
You can replace “data.xlsx” with the actual path to your Excel file and modify the word to search for according to your requirements.
- How to display different navbar component for different reactjs pages
- How to get repository id in azure devops
- How to disable liquibase in spring boot
- How to disable kafka in spring boot
- How to mock multiple usestate in jest
- How do i fix runtime error 76 path not found in excel
- How to get document id in firestore flutter
- How to change the size of alertdialog in flutter
- How to remove underline from dropdown in flutter
- How to scrape aspx page in python