Skip to content

Instantly share code, notes, and snippets.

@MS-Jahan
Created November 4, 2022 13:57
Show Gist options
  • Save MS-Jahan/63ca911a653e7225df8113d323d8f1e0 to your computer and use it in GitHub Desktop.
Save MS-Jahan/63ca911a653e7225df8113d323d8f1e0 to your computer and use it in GitHub Desktop.
Find a value in excel file using Python Pandas
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