Subsetting a pandas DataFrame by row and column number is a common operation in data analysis. Here's an example of how to subset a DataFrame by row and column number:
import pandas as pd# Create a sample DataFramedf = pd.DataFrame({ 'col1': ['A', 'B', 'C', 'D', 'E'], 'col2': [1, 2, 3, 4, 5], 'col3': ['alpha', 'beta', 'gamma', 'delta', 'epsilon']})# Subset the DataFrame by row and column numbersubset_df = df.iloc[1:4, 0:2]# Print the subset DataFrameprint(subset_df) |
In this example, we create a sample DataFrame with three columns (col1, col2, and col3) and five rows. We can subset the DataFrame using the iloc method, which allows us to select rows and columns by their integer index.
In the iloc method, the first argument specifies the rows to include, and the second argument specifies the columns to include. In this example, we subset the DataFrame to include rows 1-3 (index 1, 2, and 3) and columns 0-1 (index 0 and 1).
Finally, we print the subset DataFrame to the console.
This will output the following:
col1 col21 B 22 C 33 D 4 |
This shows us that the subset DataFrame includes only the rows and columns specified by the iloc method.