'Filtering a dataframe column with another column in a separate dataframe
I have a dataframe table (table_1) that contains 3 cloumns:
| Iteam ID | Date / time | Date / time |
|---|---|---|
| 12 | 2022-03-21 - 00:27:00 | 2022-03-21 - 00:28:00 |
| 99 | 2022-03-21 - 00:25:00 | 2022-03-21 - 00:26:00 |
| 34 | 2022-03-21 - 00:22:00 | 2022-03-21 - 00:23:00 |
| 28 | 2022-03-21 - 00:21:00 | 2022-03-21 - 00:23:00 |
I want to be able to filter the column Iteam Id with the data in another data-frame column (table_2).
| Iteam Id |
|---|
| 12 |
| 34 |
| 28 |
So the output would be:
| Iteam ID | Date / time | Date / time |
|---|---|---|
| 12 | 2022-03-21 - 00:27:00 | 2022-03-21 - 00:28:00 |
| 34 | 2022-03-21 - 00:22:00 | 2022-03-21 - 00:23:00 |
| 28 | 2022-03-21 - 00:21:00 | 2022-03-21 - 00:23:00 |
I have tryed creating a new dataframe using:
new_df = table_1[table_1['Iteam ID'].isin(table_2)
But it is returning an empty dataframe, any help with this would be much appreciated!
Solution 1:[1]
You should use the correct column instead of the whole dataframe:
new_df = table_1[table_1['Iteam ID'].isin(table_2['Iteam ID'].values)]
Solution 2:[2]
Just merge (inner join) them :
out = df1.merge(df2, on='Iteam Id')
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Z Li |
| Solution 2 | eshirvana |
