loc function in pandas do?loc is used to select rows and columns by their labels (names) in a DataFrame.
loc?You use df.loc['A'] to get the row labeled 'A'.
loc?Use df.loc[['A', 'B'], ['X', 'Y']] to select those rows and columns by label.
loc with a label that does not exist?It raises a KeyError because the label is not found in the DataFrame index or columns.
loc select rows using a label range like 'A' to 'C'?Yes, loc supports label slicing, so df.loc['A':'C'] selects rows from 'A' to 'C' inclusive.
df.loc['row1'] return?loc selects rows by label, so it returns the row labeled 'row1'.
loc uses labels, so you pass lists of row and column labels.
loc with a missing label?Using a label not in the index or columns raises a KeyError.
loc supports label slicing and includes the end label.
df.loc[:, 'col1'] select?The colon : means all rows, so this selects all rows in column 'col1'.
loc to select specific rows and columns by their labels in a pandas DataFrame.loc.