Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

204
Views
Lookup Values by Corresponding Column Header in Pandas 1.2.0 or newer

The operation pandas.DataFrame.lookup is "Deprecated since version 1.2.0", and has since invalidated a lot of previous answers.

This post attempts to function as a canonical resource for looking up corresponding row col pairs in pandas versions 1.2.0 and newer.

Some previous answers to this type of question (now deprecated):

  1. Vectorized lookup on a pandas dataframe
  2. Python Pandas Match Vlookup columns based on header values
  3. Using DataFrame.lookup to get rows where columns names are a subset of a string
  4. Python: pandas: match row value to column name/ key's value

Some Current Answers to this Question:

  1. Reference DataFrame value corresponding to column header
  2. Pandas/Python: How to create new column based on values from other columns and apply extra condition to this new column

Standard LookUp Values With Default Range Index

Given the following DataFrame:

df = pd.DataFrame({'Col': ['B', 'A', 'A', 'B'],
                   'A': [1, 2, 3, 4],
                   'B': [5, 6, 7, 8]})
  Col  A  B
0   B  1  5
1   A  2  6
2   A  3  7
3   B  4  8

I would like to be able to lookup the corresponding value in the column specified in Col:

I would like my result to look like:

  Col  A  B  Val
0   B  1  5    5
1   A  2  6    2
2   A  3  7    3
3   B  4  8    8

Standard LookUp Values With a Non-Default Index

Non-Contiguous Range Index

Given the following DataFrame:

df = pd.DataFrame({'Col': ['B', 'A', 'A', 'B'],
                   'A': [1, 2, 3, 4],
                   'B': [5, 6, 7, 8]}, 
                  index=[0, 2, 8, 9])

  Col  A  B
0   B  1  5
2   A  2  6
8   A  3  7
9   B  4  8

I would like to preserve the index but still find the correct corresponding Value:

  Col  A  B  Val
0   B  1  5    5
2   A  2  6    2
8   A  3  7    3
9   B  4  8    8

MultiIndex

df = pd.DataFrame({'Col': ['B', 'A', 'A', 'B'],
                   'A': [1, 2, 3, 4],
                   'B': [5, 6, 7, 8]},
                  index=pd.MultiIndex.from_product([['C', 'D'], ['E', 'F']]))

    Col  A  B
C E   B  1  5
  F   A  2  6
D E   A  3  7
  F   B  4  8

I would like to preserve the index but still find the correct corresponding Value:

    Col  A  B  Val
C E   B  1  5    5
  F   A  2  6    2
D E   A  3  7    3
  F   B  4  8    8

LookUp with Default For Unmatched/Not-Found Values

Given the following DataFrame

df = pd.DataFrame({'Col': ['B', 'A', 'A', 'C'],
                   'A': [1, 2, 3, 4],
                   'B': [5, 6, 7, 8]})

  Col  A  B
0   B  1  5
1   A  2  6
2   A  3  7
3   C  4  8  # Column C does not correspond with any column

I would like to look up the corresponding values if one exists otherwise I'd like to have it default to 0

  Col  A  B  Val
0   B  1  5    5
1   A  2  6    2
2   A  3  7    3
3   C  4  8    0  # Default value 0 since C does not correspond

LookUp with Missing Values in the lookup Col

Given the following DataFrame:

   Col  A  B
0    B  1  5
1    A  2  6
2    A  3  7
3  NaN  4  8  # <- Missing Lookup Key

I would like any NaN values in Col to result in a NaN value in Val

   Col  A  B  Val
0    B  1  5  5.0
1    A  2  6  2.0
2    A  3  7  3.0
3  NaN  4  8  NaN  # NaN to indicate missing
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Another option is to build a tuple of the lookup columns, pivot the dataframe, and select the relevant columns with the tuples:

cols = [(ent, ent) for ent in df.Col.unique()]

df.assign(Val = df.pivot(index = None, columns = 'Col')
                  .reindex(columns = cols)
                  .ffill(axis=1)
                  .iloc[:, -1])

  Col  A  B  Val
0   B  1  5  5.0
2   A  2  6  2.0
8   A  3  7  3.0
9   B  4  8  8.0
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!