简体   繁体   中英

How to extract the specific data from the unorganized excel file without columns)

I reached my limit and My hair is getting thinner. I really need your help.

1. Try

I'd like to extract the data line including the specific words " Super Banana " from *.xlsx in one folder.

Here is the file pic. [1]: https://i.stack.imgur.com/Cb3yD.png

But

2. Problem

  • No columns in this unorganized excel files. There are many files and I can`t add column manually to all files.

    I was looking for the way to extract:

  1. the row including the key words " Super Banana "

    or

  2. the row before and after the row including the key words

https://i.stack.imgur.com/Cb3yD.png

  • A1 title "Monday Shopping List 2020 " will be changed (Monday Shopping List 2020,Monday Shopping List 2021...)

3. My Code

import glob

files = glob.glob('*.xlsx')
print(files)

import pandas as pd
for file in files:
    df = pd.read_excel(file).fillna(value = 0)
    for row in df.values:
        data = df[df[''].str.contains('Super Banana',na=False)]
        data.to_excel('excel-data_find.xlsx', encoding='utf-8')
        print(data)
        print('Data was extracted')

use the proper file path. Example: df = pd.read_excel('C:\\Users\\file.xlsx').fillna(value = 0)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM