0

Have two df with values


df 1 


number    1          2     3

12354    mark        24  london
12356    jacob       25  denver
12357    luther      26  berlin
12358    john        27  tokyo
12359    marshall    28  cairo
12350    ted         29  delhi

another df 2

number       

12354   

12357    

12359    


remove all the rows in df1 having values of same column values of df2

Expected Output


0          1          2     3

12356    jacob       25  denver
12358    john        27  tokyo
12350    ted         29  delhi
  • 4
    df1[~df1.number.isin(df2.number)] – BENY Dec 12 '19 at 15:57
  • @anky_91 have to remove the columns based on column in df2 –  Dec 12 '19 at 15:59
  • @anky_91 edited the question can you reopen the question ? –  Dec 12 '19 at 16:05
  • 1
    `df2.merge(df1,on='number',how='outer',indicator=True).query("_merge=='right_only'")` ?? – anky Dec 12 '19 at 16:05
  • ValueError: You are trying to merge on object and int64 columns. If you wish to proceed you should use pd.concat @anky_91 –  Dec 12 '19 at 16:09
  • @user12157180 either df1['number'] or same for df2 is dtype object, can you change to int, `astype(int)` ? try with the example you have created `df1 = pd.read_clipboard()` and `df2= pd.read_clipboard()` , works for me – anky Dec 12 '19 at 16:11
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/204137/discussion-between-user12157180-and-anky-91). –  Dec 12 '19 at 16:17
  • 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) – PMende Dec 12 '19 at 16:35
  • The solution is not related the other dataframe. You want to perform a basic filtering. Just get the list of ids from the df2 and use a filtering based on the number column of df1. – anilbey Dec 12 '19 at 16:51

1 Answers1

0

Here an example :

import pandas as pd
from io import StringIO

df1 = """
number,1,2,3
12354,mark,24,london
12356,jacob,25,denver
12357,luther,26,berlin
12358,john,27,tokyo
12359,marshall,28,cairo
12350,ted,29,delhi
"""
df2 = """
number
12354
12357
12359
"""

df_df2 = pd.read_csv(StringIO(df2), sep=',')
df_df1 = pd.read_csv(StringIO(df1), sep=',')

df=pd.merge(df_df1,df_df2, indicator=True, how='outer').query('_merge=="left_only"')

df.drop(['_merge'], axis=1, inplace=True)
df.rename(columns={'number': '0'}, inplace=True)
print(df)
GiovaniSalazar
  • 1,999
  • 2
  • 8
  • 15