Skip to content
Advertisement

How to perform index/match excel function equivalent using pandas?

I am facing the below challenge.

For instance, let the dummy dataframes be,

JavaScript

Let another dataframe be,

JavaScript

The output dataframe should be the following,

JavaScript

My train of thought was to create dictionary(s), in this case, would be,

JavaScript

followed by this function,

JavaScript

I am always getting the following error,

JavaScript

Also I think this is not an efficient solution at all.

Are there pandas inbuilt function(s) to achieve the goal?

Advertisement

Answer

Use DataFrame.join with unpivot df1 by DataFrame.set_index with DataFrame.stack and rename for new column name:

JavaScript

Another idea is use DataFrame.melt, rename column and DataFrame.merge:

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement