【问题标题】:Search function through different xlsx files with pythonSearch function through different xlsx files with python
【发布时间】:2022-12-27 22:25:21
【问题描述】:

I would like to create a search function that reads several xlsx file (which can have different structure and columns) and return just the rows containing a specific value. Is it possible?

so far I used the glob function to get all the files I want to read

path = 'general path'
all_files = glob.glob(path + "/*.xlsx")
all_files

Then I tried using li = [pd.read_excel(filename, index_col=None, header=0) for filename in all_files]

to get all the rows together but I don't know how to continue and I don't think this is the best way to search for the info I need. Note: the specific value can be in any position of the row, in other words, there is no specific column where to look for, I have to check the whole row

【问题讨论】:

  • First figure out how to search in a single file. Then do this for all files

标签: python search


【解决方案1】:

Yes, it is possible to search through several Excel files and return rows containing a specific value. One approach you can take is to use the pandas library to read in the Excel files and search through their data.

Here is an example of how you can do this:

import glob
import pandas as pd

# Get a list of all the Excel files in the specified path
path = 'general path'
all_files = glob.glob(path + "/*.xlsx")

# Read in the data from all the Excel files
dfs = [pd.read_excel(filename, index_col=None, header=0) for filename in all_files]

# Concatenate the data from all the Excel files into a single DataFrame
df = pd.concat(dfs, ignore_index=True)

# Search for rows containing the specific value
value = 'specific value'
matching_rows = df[df.isin([value]).any(axis=1)]

# Print the matching rows
print(matching_rows)

This code reads in the data from all the Excel files in the specified path, concatenates the data into a single DataFrame, and then searches for rows containing the specific value. The isin method is used to check if the value is present in any of the cells in the row, and the any method is used to check if any of the cells in the row contain the value. Finally, the matching rows are printed to the output.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-11-09
    • 1970-01-01
    • 2022-12-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-02
    相关资源
    最近更新 更多