papabiceps
papabiceps

Reputation: 1058

Match two dataframes columnwise and extract values from another column in a particular order

I have two dataframes that look like this

df1

    fileName    Text
0    file1        A
1    file2        B
2    file3        C
3    file4        D
4    file5        E


df2 

    fileName    Value
0    file3        0
1    file2        1
2    file1        1
3    file5        0
4    file6        1

The contents of df1.fileName is equal to contents of df2.fileName but the order is jumbled in df2. I want to extract the value column in the order of df1.fileName.

Upvotes: 0

Views: 25

Answers (1)

jezrael
jezrael

Reputation: 862691

If duplicates in df2['fileName'] then create ordered categorical column, sorting and select Value:

print (df2)
  fileName  Value
0    file3      0
1    file2      1
2    file1      1
3    file5      0
4    file4      1
5    file1      1
6    file3      1


df2['fileName'] = pd.Categorical(df2['fileName'], categories=df1['fileName'], ordered=True)
out = df2.sort_values('fileName')['Value']
print (out)
2    1
5    1
1    1
0    0
6    1
4    1
3    0
Name: Value, dtype: int64

Upvotes: 1

Related Questions