To select specific rows from a pandas DataFrame, you can use various methods, including:
- Using
.loc[]: This method allows you to select rows by label (index). - Using
.iloc[]: This method allows you to select rows by integer position. - Using boolean indexing: This method allows you to select rows based on conditions.
1. Using .loc[]
You can select rows by their index labels:
import pandas as pd
# Create a sample DataFrame
data = {
'column1': [1, 2, 3, 4, 5],
'column2': ['A', 'B', 'C', 'D', 'E']
}
df = pd.DataFrame(data)
# Select specific rows by index labels
selected_rows = df.loc[1:3] # Selects rows with index 1, 2, and 3
print(selected_rows)
Output:
column1 column2
1 2 B
2 3 C
3 4 D
2. Using .iloc[]
You can select rows by their integer position:
# Select specific rows by integer position
selected_rows = df.iloc[1:4] # Selects rows at positions 1, 2, and 3
print(selected_rows)
Output:
column1 column2
1 2 B
2 3 C
3 4 D
3. Using Boolean Indexing
You can select rows based on conditions:
# Select rows where column1 is greater than 2
selected_rows = df[df['column1'] > 2]
print(selected_rows)
Output:
column1 column2
2 3 C
3 4 D
4 5 E
Summary
- Use
.loc[]for label-based indexing. - Use
.iloc[]for position-based indexing. - Use boolean indexing for conditional selection.
These methods provide flexibility in selecting specific rows from a DataFrame based on your needs.
