Home > OS >  How to remove the different rows in Pandas
How to remove the different rows in Pandas

Time:09-07

I have two DataFrames df1 and df2:

>>df1 
 Index X Y
   1   1 3
   2   2 4
   3   3 5
   4   4 6
>>df2
 Index
   2
   4

I want something like:

>>df3
 Index X Y
  2   2 4 
  4   4 6

How could I remove all the rows with different Index?

CodePudding user response:

I'm not sure if Index is a column of df1 and df2 or the actual index. If it's a column like X or Y then the following will work

df1 = pd.DataFrame({'Index': [1, 2, 3, 4], 'X': [1, 2, 3, 4], 'Y': [3, 4, 5, 6]})
df2 = pd.DataFrame({'Index': [2, 4]})

df3 = df1.loc[df1.Index.isin(df2.Index)]

If it's the actual table index then the following will work

df1 = pd.DataFrame(
    data={'X': [1, 2, 3, 4], 'Y': [3, 4, 5, 6]},
    index=[1, 2, 3, 4], 
)

df2 = pd.DataFrame(
    data={},
    index=[2, 4],
)

df3 = df1.loc[df1.index.intersection(df2.index)]
  • Related