Selecting DataFrame rows and columns simultaneously
There are many ways to select rows and columns. The easiest method to select one or more columns from a DataFrame is to index off of the DataFrame. However, this approach has a limitation. Indexing directly on a DataFrame does not allow you to select both rows and columns simultaneously. To select rows and columns, you will need to pass both valid row and column selections separated by a comma to either .iloc
or .loc
.
The generic form to select rows and columns will look like the following code:
df.iloc[row_idxs, column_idxs]
df.loc[row_names, column_names]
Where row_idxs
and column_idxs
can be scalar integers, lists of integers, or integer slices. While row_names
and column_names
can be the scalar names, lists of names, or names slices, row_names
can also be a Boolean array.
In this recipe, each step shows a simultaneous row and column selection using both .iloc
and .loc
.