Created
November 4, 2022 13:57
-
-
Save MS-Jahan/63ca911a653e7225df8113d323d8f1e0 to your computer and use it in GitHub Desktop.
Find a value in excel file using Python Pandas
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pandas as pd | |
from pprint import pprint | |
file = '/home/<user>/Downloads/excel_file.xlsx' | |
df = pd.read_excel(file) | |
search_keyword = 'John' | |
def get_row(df, index): | |
result_dict = {} | |
for col in df: | |
result_dict[df[col].name] = df[col].values[index] | |
return result_dict | |
def search_in_excel(df, keyword): | |
result = [] | |
for index, row in df.iterrows(): | |
for col in df: | |
if keyword in str(row[col]).strip(): | |
temp = {} | |
temp[df[col].name] = get_row(df, index) | |
result.append(temp) | |
if len(result) == 0: | |
return None | |
return result | |
pprint(search_in_excel(df, search_keyword)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment