df1:
A | B |
---|---|
1 | 10 |
2 | 20 |
3 | 30 |
4 | 40 |
5 | 50 |
df2:
A | D |
---|---|
2 | D1 |
4 | D2 |
100 | D3 |
Here is the code to create:
import pandas as pd
df1 = pd.DataFrame({'A':[1,2,3,4,5],
'B':[10,20,30,40,50]})
df2 = pd.DataFrame({'A':[2,4,100],
'D':['D1','D2','D3']})
What I want to do is, check if df2['A']
has the same data with df1['A']
. If it does, then get all the row that data is in. for example. df1['A'] = 2
is also in df2['A']
. df2['A'] = 2
has D = D1
and A = 2
.(Same applies for A=4
)
What I tried:
traindatalist = [df2[df2['A']==dtA].drop('A',axis=1) for dtA in df1['A']]