There are field_1 in both tables, we want to get rows field_3 from table_2 if the field_1 shows in Table_1.

Data

Table_1:

1
2
3
4
5
6
7
| field_1 | field_2 |
|---------|:--------|
| 1 | apple |
| 1 | pearl |
| 2 | banana |
| 3 | apple |
| 4 | peach |

Table_2:

1
2
3
4
5
6
7
8
9
10
| field_1 | field_3 |
|---------|:--------|
| 2 | train1 |
| 1 | bus1 |
| 2 | train3 |
| 5 | metro1 |
| 4 | tram1 |
| 1 | bus2 |
| 5 | metro2 |
| 5 | metro3 |

In this case, the field_1 values in table_1 are 1, 2, 3, 4.

The output should be:

1
2
3
4
5
6
7
| field_1 | field_3 |
|---------|:--------|
| 2 | train1 |
| 1 | bus1 |
| 2 | train3 |
| 4 | tram1 |
| 1 | bus2 |

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import pandas as pd
table_1_path = 'path\\table_1.txt'
table_2_path = 'path\\table_2.txt'

table_1 = pd.read_csv(table_1_path)
table_2 = pd.read_csv(table_2_path)

value = table_1['field_1']
# To select rows whose column value is in an iterable, use .isin()
output = table_2.loc[table_2['field_1'].isin(value)]

print output
#-------------------------
field_1 field_3
0 2 train1
1 1 bus1
2 2 train3
4 4 tram1
5 1 bus2
[Finished in 1.0s]

References

[1] Select rows from a DataFrame based on values in a column in pandas
[2] Pandas document | Different Choices for Indexing