Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to select specific columns in Pandas

Understanding DataFrames in Pandas

Before diving into the specifics of selecting columns, it's important to understand the basic structure of a DataFrame in Pandas. A DataFrame can be thought of as a table, much like one you would find in a spreadsheet program such as Microsoft Excel. It's composed of rows and columns, where each column can be thought of as a container holding data for a specific variable, and each row represents a single record or data point.

Imagine a DataFrame as a bookshelf. Each column is like a separate shelf, and each row is a book lying across multiple shelves. To learn more about a specific topic (column), you would focus on the books on that particular shelf.

Selecting a Single Column

Selecting a single column from a DataFrame is like picking a book from one shelf. You use the name of the shelf (column) to get all the books (data) from that shelf. In Pandas, you can do this using square brackets [] and the column name as a string (text inside quotes).

import pandas as pd

# Sample DataFrame
data = {'Name': ['Alice', 'Bob', 'Charlie'],
        'Age': [25, 30, 35],
        'City': ['New York', 'Los Angeles', 'Chicago']}
df = pd.DataFrame(data)

# Selecting the 'Name' column
name_column = df['Name']

This will output:

0      Alice
1        Bob
2    Charlie
Name: Name, dtype: object

Selecting Multiple Columns

If you want to select more than one column, it's like picking multiple books from different shelves. In Pandas, you can do this by passing a list of column names into the square brackets. A list is like a shopping basket where you can put multiple items (column names).

# Selecting 'Name' and 'City' columns
selected_columns = df[['Name', 'City']]

This will output:

      Name        City
0    Alice    New York
1      Bob  Los Angeles
2  Charlie     Chicago

Using the .loc and .iloc Methods

Pandas provides two other methods for selecting data, .loc and .iloc. You can think of .loc as using labels to find your data, like using a labeled map to find destinations. On the other hand, .iloc is like using coordinates, where you specify the numeric position in the DataFrame.

The .loc Method

The .loc method allows you to select columns by their names (labels).

# Selecting 'Age' column using .loc
age_column_loc = df.loc[:, 'Age']

This will output:

0    25
1    30
2    35
Name: Age, dtype: int64

The .iloc Method

The .iloc method is used to select columns by their integer position.

# Selecting the first and third column using .iloc
selected_columns_iloc = df.iloc[:, [0, 2]]

This will output:

      Name      City
0    Alice  New York
1      Bob  Los Angeles
2  Charlie   Chicago

Conditional Selection

Sometimes, you want to pick books based on their content, not just their location on the shelf. Similarly, in Pandas, you might want to select columns based on the data they contain. This is known as conditional selection.

# Selecting people older than 30
older_than_30 = df[df['Age'] > 30]

This will output:

      Name  Age     City
2  Charlie   35  Chicago

Using Methods to Select Columns

Pandas also provides methods to select columns based on certain criteria, such as data type.

The .select_dtypes() Method

If you want to select all columns of a particular data type, like picking all hardcover books from your bookshelf, you can use the .select_dtypes() method.

# Selecting only the numeric columns
numeric_columns = df.select_dtypes(include=[int, float])

This will output:

0   25
1   30
2   35

Renaming Columns

Sometimes, the labels on your bookshelf might not be what you want, and you'd prefer to change them. In Pandas, you can rename columns using the .rename() method.

# Renaming the 'Name' column to 'FirstName'
df_renamed = df.rename(columns={'Name': 'FirstName'})

This will output:

  FirstName  Age        City
0     Alice   25    New York
1       Bob   30  Los Angeles
2   Charlie   35     Chicago


Selecting specific columns in Pandas is a fundamental skill for data manipulation and analysis. It's like knowing how to pick the right books from your bookshelf to gather information on a particular topic. Whether you're using square brackets, .loc, .iloc, or other methods, Pandas offers a versatile set of tools for accessing the data you need. Remember, practice makes perfect. As you work more with data, these concepts will become second nature, and you'll be able to select columns in your DataFrame as easily as picking your favorite book from the shelf. Keep experimenting with different datasets and techniques, and soon you'll be a Pandas pro!