0

I have two dataframes:

First:

                         tif_pobrany
0  65926_504019_N-33-127-B-d-3-4.tif
1  65926_504618_N-33-139-D-b-1-3.tif
2  65926_504670_N-33-140-A-a-2-3.tif
3   66533_595038_N-33-79-C-b-3-3.tif
4   66533_595135_N-33-79-D-d-3-4.tif

Second:

                                                 url            godlo  ... row_num                        nazwa_tifa
0  https://opendata.geoportal.gov.pl/ortofotomapa...  M-34-68-C-a-1-2  ...   48004  73231_904142_M-34-68-C-a-1-2.tif
1  https://opendata.geoportal.gov.pl/ortofotomapa...  M-34-68-C-a-3-1  ...   48011  73231_904127_M-34-68-C-a-3-1.tif
2  https://opendata.geoportal.gov.pl/ortofotomapa...  M-34-68-C-a-3-2  ...   48012  73231_904336_M-34-68-C-a-3-2.tif
3  https://opendata.geoportal.gov.pl/ortofotomapa...  M-34-68-C-a-3-3  ...   48013  73231_904286_M-34-68-C-a-3-3.tif
4  https://opendata.geoportal.gov.pl/ortofotomapa...  M-34-68-C-a-4-2  ...   48016  73231_904263_M-34-68-C-a-4-2.tif

How can I delete rows in second dataframe which have the same 'nazwa_tifa' like in the first dataframe 'tif_pobrany'?

Something like this:

for index, row in second.iterrows():
    for index2, row2 in first.iterrows():
        if row['nazwa_tifa'] == row2['tif_pobrany']:
            del row

but it didn't work.

  • Hello. please check my answer and let me know if it fixed your issue! – Tabaraei Apr 13 '21 at 11:37
  • Does this answer your question? [In Pandas, how to delete rows from a Data Frame based on another Data Frame?](https://stackoverflow.com/questions/39880627/in-pandas-how-to-delete-rows-from-a-data-frame-based-on-another-data-frame) – Rivers Apr 13 '21 at 11:41

2 Answers2

0

considering df1 and df2 are names of your dataframes respectively:

df2 = df2[df2['nazwa_tifa'].isin(df1['tif_pobrany'])]

How it works?

  • The isin function is used to check whether values inside a Pandas series are present in another Pandas series.
  • Then, an array of True/False values are passed to df2, so it selects only rows wherever the condition is True.
  • Finally, an assignment is used to replace df2 with the new dataframe.
Tabaraei
  • 445
  • 2
  • 7
  • 18
0

Try this with your data:

import pandas as pd
df1 = pd.DataFrame({"col1":[1,2,3,4,5]})
df2 = pd.DataFrame({"col2":[1,3,4,9,8]})


df1.drop(df1[df1.col1.isin(df2.col2)].index, inplace = True)
print(df1)

output:

   col1
1     2
4     5
Vedank Pande
  • 436
  • 3
  • 10