Python 如何访问外部数据帧中的数据以返回到主数据帧中

Python 如何访问外部数据帧中的数据以返回到主数据帧中,python,pandas,dataframe,Python,Pandas,Dataframe,我有以下三个数据帧: Zip数据帧: Zip State 12345 NY 54321 CA ID Zip a1 12345 b2 54321 位置数据帧: Zip State 12345 NY 54321 CA ID Zip a1 12345 b2 54321 TestData数据帧: LocationID About a1 Here is location a b2

我有以下三个数据帧:

Zip数据帧:

Zip    State
12345   NY
54321   CA
ID    Zip
a1   12345
b2   54321
位置数据帧:

Zip    State
12345   NY
54321   CA
ID    Zip
a1   12345
b2   54321
TestData数据帧:

   LocationID    About
   a1            Here is location a
   b2            Here is location b

如何在TestData数据框中使用相应的状态数据创建一个新列“State”?您可以使用LocationID在locations df中查找zip,然后使用邮政编码在zip数据框中查找正确的状态

让我知道这是否有效:

data=pd.merge(pd.merge(TestData, Locations , right_on='ID', left_on='LocationID'),Zip ,on='Zip')
data.drop(columns=['Zip','ID'],inplace=True)