Home > Enterprise >  Index Match / VLookup a single value from a data frame to a variable
Index Match / VLookup a single value from a data frame to a variable

Time:08-19

I'm looking to create a new variable in python indexed from a pandas data frame... I'd use Index/Match or VLookup for this function in excel.

Table (in the form of a df in python):

ID       Date
0        8-18-22
1        7-18-22
2        6-20-18
3        7-04-19

I want to be able to plug in an ID and return a Date as a new variable

This seems super simple... but I've searched for hours on how to do this and I can't figure it out.

---------------- EDIT ----------------- 8-18-22 12:37 CST

Based on answers I realized I need to edit this question.

Say my data frame looks like this:

     My_ID        DATE
0    0124        8-18-22
1    1456        7-18-22
2    2678        6-20-18
3    3589        7-04-19

I want to be able to input "My_ID" and return the DATE. Not the index of the DF number

CodePudding user response:

If the ID column has unique values i highly recommend you making it the Index column.

df.set_index("My_ID", inplace = True)

id_1456 = df.loc["1456", "DATE"] #this stores the "8-18-22" in the variable id_1456.
  • Related