Skip to content
Advertisement

Create Dataframe by calling indices of df1 that are listed in df2

I’m new to Python Pandas and struggling with the following problem for a while now.

The following dataframe df1 values show the indices that are coupled to the values of df2 that should be called

JavaScript

df2 contains the values that belong to the indices that have to be called.

JavaScript

For example, df1 shows the value ‘0’ in column ‘Name161’. Then df3 should show the value that is listed in df2 with index 0. In this case ‘164’.

Till so far, I got df3 showing the first 3 values of df2, but of course that not what I would like to achieve.

JavaScript

Any help would be much appreciated, thanks!

Advertisement

Answer

Use DataFrame.stack with Series.reset_index for reshape both DataFrames, then merging by DataFrame.merge with left join and last pivoting by DataFrame.pivot:

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