Python Forum
Partial Matching Rows In Pandas DataFrame Query - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Partial Matching Rows In Pandas DataFrame Query (/thread-34220.html)



Partial Matching Rows In Pandas DataFrame Query - eddywinch82 - Jul-08-2021

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


RE: Partial Matching Rows In Pandas DataFrame Query - eddywinch82 - Jul-08-2021

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