Python Forum
"Vlookup" in pandas dataframe
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
"Vlookup" in pandas dataframe
#1
Hi! How can I find a value in a column and return it from another column, like a vlookup? Below is an example dataframe.

import pandas as pd
]data = {"Name": ["John", "Michael", "Mary", "Jennifer"], "Age": [25,30,20,25]}
df = pd.DataFrame(data)
df 
Return John's age by searching for his name.
Reply
#2
You have some typo's in your code, and if in a module, you need print to display df.

import pandas as pd

data = {"Name": ["John", "Michael", "Mary", "Jennifer"], "Age": [25, 30, 20, 25]}
df = pd.DataFrame(data)
print(df)
There are several tutorials on 'vlookup' with pandas,
here's one: https://www.geeksforgeeks.org/how-to-do-...ng-pandas/
Reply
#3
Something like below?

import pandas as pd

data = {"Name": ["John", "Michael", "Mary", "Jennifer"], "Age": [25, 30, 20, 25]}
df = pd.DataFrame(data)

print(df[df.Name == 'John']['Age'][0])  # prints 25
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#4
import pandas as pd

data = {"Name": ["John", "Michael", "Mary", "Jennifer"], "Age": [25, 30, 20, 25]}
df = pd.DataFrame(data)
>>> df
       Name  Age
0      John   25
1   Michael   30
2      Mary   20
3  Jennifer   25
>>> 
>>> df.loc[df.Name == 'John', 'Age']
0    25
Name: Age, dtype: int64
More general search against whole DataFrame.
>>> search_name = 'Mar'
>>> df[df.apply(lambda row: row.astype(str).str.contains(search_name, case=False).any(), axis=1)]
   Name  Age
2  Mary   20
>>> df[df.apply(lambda row: row.astype(str).str.contains(search_name, case=False).any(), axis=1)].Age
2    20
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python Alteryx QS-Passing pandas dataframe column inside SQL query where condition sanky1990 0 728 Dec-04-2023, 09:48 PM
Last Post: sanky1990
  Vlookup function in Pyhton antozas 1 641 Oct-02-2023, 04:16 AM
Last Post: vikraman
  Question on pandas.dataframe merging two colums shomikc 4 829 Jun-29-2023, 11:30 AM
Last Post: snippsat
  Pandas AttributeError: 'DataFrame' object has no attribute 'concat' Sameer33 5 5,593 Feb-17-2023, 06:01 PM
Last Post: Sameer33
  help how to get size of pandas dataframe into MB\GB mg24 1 2,353 Jan-28-2023, 01:23 PM
Last Post: snippsat
  pandas dataframe into csv .... exponent issue mg24 10 1,766 Jan-20-2023, 08:15 PM
Last Post: deanhystad
  How to assign a value to pandas dataframe column rows based on a condition klllmmm 0 828 Sep-08-2022, 06:32 AM
Last Post: klllmmm
  How to retrieve records in a DataFrame (Python/Pandas) that contains leading or trail mmunozjr 3 1,747 Sep-05-2022, 11:56 AM
Last Post: Pedroski55
  Increase the speed of a python loop over a pandas dataframe mcva 0 1,314 Jan-21-2022, 06:24 PM
Last Post: mcva
  for loop in dataframe in pandas Paulman 7 2,749 Dec-02-2021, 12:15 AM
Last Post: bowlofred

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020