sayo
sayo

Reputation: 217

Reshaping data frame to extract specific results

I have two Dataframes:

import pandas as pd
d = {'0': [2154,799,1023,4724], '1': [27, 2981, 952,797],'2':[4905,569,4767,569]}
df1 = pd.DataFrame(data=d)

and

d={'PART_NO': ['J661-03982','661-08913', '922-8972','661-00352','661-06291',''], 'PART_NO_ENCODED': [2154,799,1023,27,569]}
df2 = pd.DataFrame(data=d)

I want to get the corresponding part_no for each row in df1 so the resulting data frame should look like this:

d={'PART_NO': ['J661-03982','661-00352',''], 'PART_NO_ENCODED': [2154,27,4905]}
df3 = pd.DataFrame(data=d)

How do I do this?

Upvotes: 1

Views: 31

Answers (1)

BENY
BENY

Reputation: 323316

You can do with reindex(after set_index)

df2.set_index('PART_NO_ENCODED').reindex(df1.iloc[0,:]).reset_index().rename(columns={0:'PART_NO_ENCODED'})
Out[242]: 
   PART_NO_ENCODED     PART_NO
0             2154  J661-03982
1               27   661-00352
2             4905         NaN

Upvotes: 1

Related Questions