How to extract rows from a Pandas dataframe using .loc[]?

The Pandas.loc[] method can be used to extract rows from a dataframe by giving index labels.


import pandas as pd

users = [('Ali', 'M', 'Lahore', 20000),
            ('Jhon', 'M', 'Dubai', 25000),
            ('Wick', 'M', 'London', 40000),
            ('Harry', 'M', 'Manchester', 35000),
            ('Snape', 'M', 'Hogwards', 30000),
            ('Hermoine', 'F', 'New York', 20000),
            ('Ayesha', 'F', 'Karachi', 24000),
            ('Julie', 'F', 'Miami', 70000)

#Create a sample dataframe and setting name as index column
df = pd.DataFrame(users, columns =['Name', 'Sex','City', 'Income'])
df.set_index("Name", inplace = True)
#retrieving row by loc method
data = df.loc["Snape"]