Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Partial Word Search
#1
Hello,

I have an excel file that doesn't have header names, and the data doesn't start till row 8. I can't add data to rows 1 -7.

I want to do a partial word search on column A, and copy the whole row of data that contains that partial word to a new excel file. I tried the below code but I'm doing something wrong because it comes back with
KeyError A

import pandas as pd

df = pd.read_excel('C:\\data\\Report1.xlsx', sheet_name = 'data1')

df = df[df["A"].str.contains('Down*')]
Reply
#2
Do you want the asterisk * to be part of the string you're searching for? Or are you intending that to be like a glob pattern and match anything afterword?

Right now I don't know what part is failing for you. You might want to break it up so that if the match succeeds, you print a message and see if the match part is working. Then if that's okay work on getting the sheet updated.
Reply
#3
(Jun-29-2020, 08:10 PM)bowlofred Wrote: Do you want the asterisk * to be part of the string you're searching for? Or are you intending that to be like a glob pattern and match anything afterword?

Right now I don't know what part is failing for you. You might want to break it up so that if the match succeeds, you print a message and see if the match part is working. Then if that's okay work on getting the sheet updated.

So, I ended up figuring out part of it. I added header=7 so that it would start looking at row 7 where the headers begin. Then I was looking for anything that contains the word "down" that's why I used the asterisk. This worked great, now I'm at the part where I need to copy the data to a new excel file. I am not sure how to do that.

The hard part is, is that I now need to copy rows-1-6 back into everything because column G row 1-6 has data I need. A- F row 1-6 is blank, G, row 1-6 has data, By adding header=7 to the script it did away with some data I needed.


import pandas as pd
 
df = pd.read_excel('C:\\data\\Report1.xlsx', sheet_name = 'data1', header=7)
 
df = df[df["A"].str.contains('Down*', na=false)]
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  partial functions before knowing the values mikisDeWitte 4 537 Dec-24-2023, 10:00 AM
Last Post: perfringo
  Move Files based on partial Match mohamedsalih12 2 748 Sep-20-2023, 07:38 PM
Last Post: snippsat
  Partial KEY search in dict klatlap 6 1,200 Mar-28-2023, 07:24 AM
Last Post: buran
  remove partial duplicates from csv ledgreve 0 746 Dec-12-2022, 04:21 PM
Last Post: ledgreve
  Webhook, post_data, GPIO partial changes DigitalID 2 955 Nov-10-2022, 09:50 PM
Last Post: deanhystad
  Optimal way to search partial correspondence in a large dict genny92c 0 975 Apr-22-2022, 10:20 AM
Last Post: genny92c
Question Problem: Check if a list contains a word and then continue with the next word Mangono 2 2,455 Aug-12-2021, 04:25 PM
Last Post: palladium
  Partial Matching Rows In Pandas DataFrame Query eddywinch82 1 2,338 Jul-08-2021, 06:32 PM
Last Post: eddywinch82
  search for more than one word using lambda illmattic 2 1,986 Nov-13-2020, 11:44 AM
Last Post: illmattic
  Complex word search multiple files Kristenl2784 0 1,556 Jul-18-2020, 01:22 PM
Last Post: Kristenl2784

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020