1

this is the df (which is a subset of a much larger df)

df = pd.DataFrame({
                   'Date': ['04/03/2020', '06/04/2020','08/06/2020','12/12/2020'],
                    'Tval' : [0.01,0.015,-0.023,-0.0005]
})

and if I need the Tval for say '06/04/2020' (just a single date i need value for). how do I get it? I know merge and join can be used to replicate vlookup function in python but what if its a single value you looking for? Whats the best way to perform the task?

rioV8
  • 24,506
  • 3
  • 32
  • 49
user13412850
  • 509
  • 1
  • 5
  • 16
  • 1
    The answers below are a little misleading. If you know that the values in „Date“ are unique, set an index via „df.set_index(„Date“, inplace=True)“ followed by df.loc[„06/04/2020“] – AlexNe Nov 03 '20 at 23:47

2 Answers2

2

Pandas docs recommend using loc or iloc for most of lookups:

df = pd.DataFrame({
                   'Date': ['04/03/2020', '06/04/2020','08/06/2020','12/12/2020'],
                    'Tval' : [0.01,0.015,-0.023,-0.0005]
})

df.loc[df.Date == '06/04/2020', 'Tval']

Here the first part of the expression in the brackets df.Date == '06/04/2020' selects an index of a row(s) you want to see and the second part specifies which column(s) you want to have displayed.

If instead you wanted to see the data for the entire row, you could re-write it as df.loc[df.Date == '06/04/2020', : ].

NotAName
  • 3,821
  • 2
  • 29
  • 44
1

Selecting in a dataframe works like this:

df.loc[df.Date == '06/06/2020', "Tval"]

The way to make sense of this is:

df.Date=='06/06/2020'

Out: 
0    False
1    False
2    False
3    False
Name: Date, dtype: bool

produces a series of True/False values showing for which rows in the Date column match the equality. If you give a DataFrame such a series, it will select out only the rows where the series is True. To see that, see what you get from:

df.loc[df.Date=='06/06/2020']

Out[]: 
         Date   Tval
2  08/06/2020 -0.023

Finally, to see the values of 'Tval' we just do:

df.loc[df.Date == '06/06/2020', "Tval"]
Metropolis
  • 2,018
  • 1
  • 19
  • 36
  • 1
    don't chain your indexing. do it all in one go: `df[df["Date"] == '06/06/2020', "Tval"]` – Paul H Nov 03 '20 at 23:39
  • 1
    Please use `.loc` for selecting via masks and labels. Your approach is called [chained indexing](https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#indexing-view-versus-copy) and is not an efficient method of selecting slices based on rows and columns https://stackoverflow.com/questions/41253170/whats-the-alternative-to-pandas-chain-indexing – Cameron Riddell Nov 03 '20 at 23:41
  • Good comments. Thanks. – Metropolis Nov 03 '20 at 23:47