WikiGalaxy

Personalize

Selecting Rows in Pandas

Using .loc[] for Label-based Indexing

The .loc[] function allows you to select rows based on labels. It's useful when you have meaningful row labels.


import pandas as pd

data = {'Name': ['Alice', 'Bob', 'Charlie'], 'Age': [25, 30, 35]}
df = pd.DataFrame(data)
selected_rows = df.loc[0:1]
print(selected_rows)
        

Using .iloc[] for Position-based Indexing

The .iloc[] function is used to select rows based on their integer position. It's helpful when the row labels are not meaningful.


import pandas as pd

data = {'Name': ['Alice', 'Bob', 'Charlie'], 'Age': [25, 30, 35]}
df = pd.DataFrame(data)
selected_rows = df.iloc[0:2]
print(selected_rows)
        

Boolean Indexing for Conditional Selection

Boolean indexing allows you to select rows based on a condition, such as selecting rows where the age is greater than 30.


import pandas as pd

data = {'Name': ['Alice', 'Bob', 'Charlie'], 'Age': [25, 30, 35]}
df = pd.DataFrame(data)
selected_rows = df[df['Age'] > 30]
print(selected_rows)
        

Using .query() for More Complex Queries

The .query() method provides a way to select rows using a query string, which can be more readable for complex conditions.


import pandas as pd

data = {'Name': ['Alice', 'Bob', 'Charlie'], 'Age': [25, 30, 35]}
df = pd.DataFrame(data)
selected_rows = df.query('Age > 30')
print(selected_rows)
        

Using .head() and .tail() for Quick Access

The .head() and .tail() methods are used to quickly access the first or last few rows of a DataFrame, respectively.


import pandas as pd

data = {'Name': ['Alice', 'Bob', 'Charlie'], 'Age': [25, 30, 35]}
df = pd.DataFrame(data)
selected_rows = df.head(2)
print(selected_rows)
        

Selecting Columns in Pandas

Selecting a Single Column by Name

You can select a column by its name, which returns a Series object. This is useful for operations on a single column.


import pandas as pd

data = {'Name': ['Alice', 'Bob', 'Charlie'], 'Age': [25, 30, 35]}
df = pd.DataFrame(data)
selected_column = df['Name']
print(selected_column)
        

Selecting Multiple Columns by List of Names

To select multiple columns, provide a list of column names. This returns a DataFrame with the selected columns.


import pandas as pd

data = {'Name': ['Alice', 'Bob', 'Charlie'], 'Age': [25, 30, 35]}
df = pd.DataFrame(data)
selected_columns = df[['Name', 'Age']]
print(selected_columns)
        

Using .iloc[] for Position-based Column Selection

The .iloc[] function can also be used to select columns based on their integer index positions.


import pandas as pd

data = {'Name': ['Alice', 'Bob', 'Charlie'], 'Age': [25, 30, 35]}
df = pd.DataFrame(data)
selected_columns = df.iloc[:, 0:1]
print(selected_columns)
        

Using .loc[] for Label-based Column Selection

The .loc[] function allows for selecting columns by label, which is useful when labels are more intuitive than indexes.


import pandas as pd

data = {'Name': ['Alice', 'Bob', 'Charlie'], 'Age': [25, 30, 35]}
df = pd.DataFrame(data)
selected_columns = df.loc[:, 'Name':'Age']
print(selected_columns)
        

Using .filter() for Column Selection with Patterns

The .filter() method is handy for selecting columns based on patterns or specific criteria, such as column names containing a certain string.


import pandas as pd

data = {'Name': ['Alice', 'Bob', 'Charlie'], 'Age': [25, 30, 35]}
df = pd.DataFrame(data)
selected_columns = df.filter(like='Name')
print(selected_columns)
        
logo of wikigalaxy

Newsletter

Subscribe to our newsletter for weekly updates and promotions.

Privacy Policy

 • 

Terms of Service

Copyright © WikiGalaxy 2025