How to take Column-Slices of DataFrame in Pandas?


Pandas, an influential Python library renowned for its data manipulation and analysis capabilities, offers an array of tools to empower data scientists and analysts alike. Among its key data structures, the DataFrame stands tall−a two−dimensional labeled data structure with columns of potentially diverse types. When traversing the vast landscape of DataFrames, it frequently becomes necessary to extract specific columns or a range of columns, an art commonly referred to as column−slicing. In this article, we embark on a journey to explore various methods that unveil the secrets of taking column−slices in Pandas. Brace yourself for an expedition through the following techniques that will revolutionize your data manipulation skills:

  • Unveiling the Magic of Column Labels

  • The Illustrious `iloc` Property: Unleashing the Power of Indices

  • The `loc` Property: A Gateway to Selective Slicing

  • Cracking the Code with the Mighty `filter()` Function

Example 1: Unveiling the Magic of Column Labels

The simplest path to traverse when selecting specific columns from a DataFrame involves utilizing the enchanting column labels. You can harness the power of square brackets `[]` and wield the column label to select a solitary column or gracefully pass a list of column labels to embrace the splendor of multiple columns.

import pandas as pd

data = {'A': [1, 2, 3],
        'B': [4, 5, 6],
        'C': [7, 8, 9]}

df = pd.DataFrame(data)

# Initiating the mystical selection of a single column
col_A = df['A']
print(col_A)

# Embracing the enchantment of multiple columns
cols_AB = df[['A', 'B']]
print(cols_AB)

Output

0    1
1    2
2    3
Name: A, dtype: int64

   A  B
0  1  4
1  2  5
2  3  6

Example 2: The Illustrious 'iloc' Property: Unleashing the Power of Indices

Behold the grandeur of the `iloc` property, a force to be reckoned with when it comes to selecting columns through the sheer might of integer indices. It shines the brightest when you seek to traverse a realm of columns or when the elusive column labels evade your knowledge.

import pandas as pd

data = {'A': [1, 2, 3],
        'B': [4, 5, 6],
        'C': [7, 8, 9]}

df = pd.DataFrame(data)

# Initiating the magnificent selection of a single column
col_A = df.iloc[:, 0]
print(col_A)

# Embracing the grandiose power of multiple columns
cols_AB = df.iloc[:, 0:2]
print(cols_AB)

Output

0    1
1    2
2    3
Name: A, dtype: int64

   A  B
0  1  4
1  2  5
2  3  6

Example 3: The 'loc' Property: A Gateway to Selective Slicing

Welcome to the realm of the `loc` property, an esteemed method to select columns based on their labels. Resonating with the first method, it grants you the ability to journey through a range of columns using the esteemed colon `:` operator.

import pandas as pd

data = {'A': [1, 2, 3],
        'B': [4, 5, 6],
        'C': [7, 8, 9]}

df = pd.DataFrame(data)

# Initiating the wondrous selection of a single column
col_A = df.loc[:, 'A']
print(col_A)

# Embracing the majesty of multiple columns
cols_AB = df.loc[:, 'A':'B']
print(cols_AB)

Output

0    1
1    2
2    3
Name: A, dtype: int64

   A  B
0  1  4
1  2  5
2  3  6

Example 4: Cracking the Code with the Mighty 'filter()' Function

Prepare to unravel the secrets of the `filter()` function—an extraordinary tool that bestows upon you the power to select columns using regular expressions. Its true strength manifests when you seek columns that bear the mark of a specific pattern within their labels.

import pandas as pd

data = {'A': [1, 2, 3],
        'B': [4, 5, 6],
        'C': [7, 8, 9]}

df = pd.DataFrame(data)

# Selecting columns blessed with labels commencing with 'A' or 'B'
cols_AB = df.filter(regex='^(A|B)')
print(cols_AB)

Output

   A  B
0  1  4
1  2  5
2  3  6

Conclusion

Throughout this awe−inspiring expedition, we have traversed the diverse methods that empower the extraction of column−slices from Pandas DataFrames. We have gracefully navigated the realm of column labels, delved into the realms of the illustrious `iloc` and `loc` properties, and finally cracked the code with the mighty `filter()` function. Each technique boasts its unique advantages and is tailored to address specific use cases. Armed with these profound insights, you are now equipped to deftly maneuver through the intricate web of DataFrame column−slicing in Pandas, conquering any data analysis task that comes your way. Unleash the power within, and let your data exploration and manipulation skills soar to new heights!"

Updated on: 10-Jul-2023

44 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements