-1

I want to short my data, the whole data shape is 30000x480. And I want to drop some rows based on the row names of another data frame.

Help me to solve it and get the solution for:

df1

Row  a  b
A    1  2
B    3  4
C    5  6
D    7  8
E    9 10
F   11 12
G   13 14

df2

Row  a  b
C    5  6
D    7  8
F   11 12
G   13 14

So, I want to delete the rows in df1 that doesn't exist on the df2, it's hard to delete as manually because the data is very big

1 Answers1

0

For better understanding, taking the same data given. Let me put the same question in a different context for a better understanding as below:

Question : Want to delete the rows in df1 that doesn't exist on the df2

New way : you need the rows of df1 that are present in df2 (or) in a way you need the common rows of both df1 & df2, try this

>>> import pandas as pd
>>> df2 = pd.DataFrame({'Row': ['C', 'D', 'F','G'], 'a': [5, 7, 11, 13], 'b' : [6, 8, 12, 14]})
>>> df1 = pd.DataFrame({'Row' : ['A', 'B', 'C', 'D'], 'a': [1,3,5,7], 'b': [2,4,6, 8]})
>>> df1
  Row  a  b
0   A  1  2
1   B  3  4
2   C  5  6
3   D  7  8
>>> df2
  Row   a   b
0   C   5   6
1   D   7   8
2   F  11  12
3   G  13  14
>>> pd.merge(df1, df2, 'inner')
  Row  a  b
0   C  5  6
1   D  7  8
>>>
Sarath Chandra Vema
  • 792
  • 1
  • 6
  • 13