Mat_nekras
Mat_nekras

Reputation: 81

Getting data frame(slicing) based on the values of column(of another data frame)

I have a list of id column from first data frame:

id = ['prime*76164862',
 'prime*40960715',
 '80006*0000000000359596',
 'gcif*103058587',
 'prime*54619204'...]

And I have the second data frame, that is bigger than first one. From second data frame(based on list id) I want to get data frame, in which id from first data frame matches to id from the second data frame. So I want to get rows of second data frame, that matches id column of first data frame The only values, that are common, are id values. How can I do it?

Upvotes: 0

Views: 44

Answers (2)

ammy
ammy

Reputation: 648

Df1 data:

df1
                       id
0          prime*76164862
1          prime*40960715
2  80006*0000000000359596
3          gcif*103058587
4          prime*54619204

df2 data:

df2
                       id
0          prime*76164862
1          prime*40960715
2  80006*0000000000359596
3          gcif*103058587
4          prime*54619204
5        prime*5461920488
6        prime*5461920444

to check isin

final_data = df1.loc[df1['id'].isin(df2['id'])]

final_data

final_data

                       id
0          prime*76164862
1          prime*40960715
2  80006*0000000000359596
3          gcif*103058587
4          prime*54619204

Upvotes: 1

AaronDT
AaronDT

Reputation: 4060

Lets say your second dataframe is called "df" and your id-column is called "ids", you can filter by your list "id" like so:

id = ['prime*76164862',
 'prime*40960715',
 '80006*0000000000359596',
 'gcif*103058587',
 'prime*54619204']

df[df['ids'].isin(id)]

Upvotes: 0

Related Questions