Python Forum
Partial Matching Rows In Pandas DataFrame Query
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Partial Matching Rows In Pandas DataFrame Query
#1
Hi there,

I have the following Part of a Python Code, which deletes DataFrame Rows from the last three Urls, if there is matching Data in the first 3 Website Urls,
DataFrame Output. However in certain Rows, there are partial matching words, i.e. in the LOCATION Column. I.e. Texel, with other words in the string, but that row won't be deleted, because it is just Texel in the other DataFrame LOCATION Column Row.

I know I can just drop Rows which I have done allready, or remove commas from DataFrame Rows, earlier on in the Code, which would make filtering the data simpler.

However I was wondering, how to do the following things, how to ignore the Comma after the word, and how to code, if the first 4 letters of the first word, or if I wanted the last 4 letters instead, in the LOCATION Column DataFrame Rows string, match the first word in a row in the other DataFrame, delete those rows from the chosen DataFrame ?

Here is the part of the Code, I mentioned :-

remove=[]
for i,row1 in final_df_.iterrows():
    loc = row1[0]
    date = row1[1]
    for j,row2 in final_df.iterrows():
        if loc in row2[0] and date==row2[1]:
            remove.append(i)
            
final_df_=final_df_.drop(final_df_.index[remove])
final_df_.reset_index(drop=True,inplace=True)
full_df=pd.concat([final_df,final_df_],axis=0)
Any help anyone could give me, would be much appreciated.

Best Regards

Eddie Winch Smile
Reply
#2
I have modified that part of the Code, to the following :-

remove=[]
for i,row1 in final_df_.iterrows():
    loc = row1[0] and row1[0][:4]
   #loc = row1[0] and row1[0][-4:]
    date = row1[1]
    for j,row2 in final_df.iterrows():
         if loc in row2[0] and date==row2[1] or loc in row2[0][:4] and date==row2[1]:
        #if loc in row2[0] and date==row2[1] or loc in row2[0][-4:] and date==row2[1]:   
            remove.append(i)
            
final_df_=final_df_.drop(final_df_.index[remove])
final_df_.reset_index(drop=True,inplace=True)
full_df=pd.concat([final_df,final_df_],axis=0)
This matches the first word in rows in the other DataFrame, where the first 4 letters are the same.

The hashed out lines of code, does the same, but for the last 4 letters of the first word in rows.

Anyone any ideas, how to ignore the comma, at the end of the first word in the rows ?

Regards

Eddie Winch
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Webhook, post_data, GPIO partial changes DigitalID 2 195 Nov-10-2022, 09:50 PM
Last Post: deanhystad
  How to assign a value to pandas dataframe column rows based on a condition klllmmm 0 260 Sep-08-2022, 06:32 AM
Last Post: klllmmm
  How to retrieve records in a DataFrame (Python/Pandas) that contains leading or trail mmunozjr 3 443 Sep-05-2022, 11:56 AM
Last Post: Pedroski55
  "Vlookup" in pandas dataframe doug2019 3 864 May-09-2022, 01:35 PM
Last Post: snippsat
  Optimal way to search partial correspondence in a large dict genny92c 0 406 Apr-22-2022, 10:20 AM
Last Post: genny92c
  Regex Expression With Code Query In Pandas eddywinch82 8 1,302 Apr-13-2022, 09:12 AM
Last Post: snippsat
  Increase the speed of a python loop over a pandas dataframe mcva 0 759 Jan-21-2022, 06:24 PM
Last Post: mcva
  The code I have written removes the desired number of rows, but wrong rows Jdesi1983 0 1,014 Dec-08-2021, 04:42 AM
Last Post: Jdesi1983
  for loop in dataframe in pandas Paulman 7 1,438 Dec-02-2021, 12:15 AM
Last Post: bowlofred
  rows from sql query need to write to a file as columns sjcsvatt 6 1,437 Oct-09-2021, 12:45 AM
Last Post: snippsat

Forum Jump:

User Panel Messages

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