Conditional Search Multiple Excel Files

From rbachwiki
Jump to navigation Jump to search
import pandas as pd
import numpy as np

#excel_file = 'search/poke_1.xlsx'
#df = pd.read_excel(excel_file)
## Print just one column
#print(df['Name'].where(df['Type 2']=='Poison'))

## Print multiple columns that match search criteria
#poison = df[['Name', 'Type 1', 'Attack', 'Type 2']].where(df['Type 2']=='Poison')
#print(poison.dropna())
## search through multiple excel files

all_files= ['search/poke_1.xlsx','search/poke_2.xlsx','search/poke_3.xlsx']

# Create a data frame to hold the search results
data = pd.DataFrame()

for individual_file in all_files:
    df = pd.read_excel(individual_file)
    poison = df[['Name', 'Type 1', 'Attack', 'Type 2']].where(df['Type 2']=='Poison').dropna()
    print("File Name" + individual_file)
    print(poison)
    
    ## create new excel file
    data = data.append(poison)
    
    
data.to_excel("search/combined.xlsx")
# Gives you Record count
data.shape 

Back To Top - Main Category/ Excel Category