Difference between revisions of "Conditional Search Multiple Excel Files"
Jump to navigation
Jump to search
(Created page with "<pre> </pre> ==Back To Top - Category== Category:Python") |
|||
(One intermediate revision by the same user not shown) | |||
Line 1: | Line 1: | ||
<pre> | <pre> | ||
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 | |||
</pre> | </pre> | ||
==[[#top|Back To Top]] - [[Python|Category]]== | ==[[#top|Back To Top]] - [[Python|Main Category]]/[[Python_Excel_Related| Excel Category]]== | ||
[[Category:Python]] | [[Category:Python]] |
Latest revision as of 15:54, 3 September 2020
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