Skip to content
Advertisement

Better solution to index a DataFrame according to the values of 2 others

I would like to index a DataFrame (aaxx_df) according to the values of 2 others (val1_df for the columns and val2_df for the rows).

I put below a solution that works for my problem, but I guess, there must be some much cleaner solutions, possibly via SQL (it seems to me to be very similar to a relational database problem).

Advertisement

Answer

If you want to index aaxx_df using the values from val2_df (rows) and val1_df (cols), use lookup:

Output


Since Dec 26, 2020, with pandas 1.2, the lookup method is deprecated. Here is a minimal version as a function:

The code becomes thus:

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