pandas.DataFrame.loc() function with example in python
pandas.DataFrame.loc(): This function used access a group of rows and columns by label(s) or a boolean array.
.loc is primarily label based, but may also be used with a boolean array.
Allowed inputs are:
A single label, e.g. 5 or ‘a’, (note that 5 is interpreted as a label of the index, and never as an integer position along the index).
A list or array of labels, e.g. [‘a’, ‘b’, ‘c’].
A slice object with labels, e.g. ‘a’:’f’.
A boolean array of the same length as the axis being sliced, e.g. [True, False, True].
A callable function with one argument (the calling Series, DataFrame or Panel) and that returns valid output for indexing (one of the above)
See more at Selection by Label
when any items are not found
import pandas as pd df = pd.DataFrame([[1, 2], [4, 5], [7, 8]], index=['raju', 'ravi', 'dani'], columns=['exp', 'salary']) print(df.loc['raju']) print(df.loc[['raju','ravi']])
Name: raju, dtype: int64
raju 1 2
ravi 4 5