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
  for loop in dataframe in pandas Paulman 7 206 Dec-02-2021, 12:15 AM
Last Post: bowlofred
  rows from sql query need to write to a file as columns sjcsvatt 6 417 Oct-09-2021, 12:45 AM
Last Post: snippsat
  Structuring and pivoting corrupted dataframe in pandas gunner1905 2 406 Sep-18-2021, 01:30 PM
Last Post: gunner1905
  TypeError: 'DataFrame' object is not callable using Pandas in Python sofiavlachou 1 1,404 Sep-02-2021, 03:24 PM
Last Post: buran
  Problem in saving .xlsm (excel) file using pandas dataframe in python shantanu97 2 689 Aug-29-2021, 12:39 PM
Last Post: snippsat
  How to combine multiple rows of strings into one using pandas? shantanu97 1 903 Aug-22-2021, 05:26 AM
Last Post: klllmmm
  Merging spreadsheets with the same columns and extracting rows with matching entries johnbernard 3 595 Aug-19-2021, 03:08 PM
Last Post: johnbernard
  Python Pandas: How do I sumproduct by rows with an if condition? JaneTan 2 1,004 Jul-13-2021, 11:36 AM
Last Post: jefsummers
  Pandas dictionary dataframe help michaelserra 4 731 Jun-19-2021, 10:26 AM
Last Post: michaelserra
  Pandas DataFrame combine rows by column value, where Date Rows are NULL rhat398 0 699 May-04-2021, 10:51 PM
Last Post: rhat398

Forum Jump:

User Panel Messages

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