Pandas Dataframe Loc

Use df.loc[row, column] to access specific cells. For example, df.loc['Row_2', 'Name'] returns the value at row 'Row_2' and column 'Name'.

Basic Syntax

You can select multiple rows and columns using lists. For example, df.loc[['Row_1', 'Row_2'], ['Age', 'Name']] returns the specified rows and columns.

Selecting Rows and Columns

Use slice notation to select a range of rows and columns. For example, df.loc['Row_1':'Row_3', 'Age':'Name'] includes both start and end labels.

Slicing Rows and Columns

Use boolean arrays to select rows based on conditions. For example, df.loc[df['Age'] > 30] returns rows where the 'Age' is greater than 30.

Boolean Indexing

You can set values using loc. For example, df.loc['Row_2', 'Name'] = 'New Name' updates the value at the specified cell.

Setting Values

Common use cases include selecting specific data for analysis, updating values, and handling missing data.

Practical Examples