Select Multiple Columns in Pandas DataFrame

Mastering Select Multiple Columns in Pandas DataFrame

Extracting data from multiple columns in a Pandas DataFrame might seem tricky if you’re used to treating DataFrames like simple 2D arrays. However, Pandas offers streamlined methods to efficiently select multiple columns in Pandas DataFrame. Let’s explore these techniques:

Key Methods for Column Selection:

  • Basic Indexing (getitem syntax): Pass a list of column names within square brackets [].
  • iloc() Function: Leverage integer-based indexing to select columns by their positions.
  • loc() Function: Select columns using their labels (column names).

Illustrative Example:

import pandas as pd
import numpy as np

df = pd.DataFrame(np.random.rand(4,4), columns = ['a','b','c','d'])
print(df)

#Output:
#          a         b         c         d
#0  0.255086  0.282203  0.342223  0.263599
#1  0.744271  0.591687  0.861554  0.871859
#2  0.420066  0.713664  0.770193  0.207427
#3  0.014447  0.352515  0.535801  0.119759

1. Select Multiple Columns Using __getitem__ Syntax

Store the desired column names in a list and pass it within the []:

print(df[['a','c']])

#Output:
#          a         c
#0  0.255086  0.342223
#1  0.744271  0.861554
#2  0.420066  0.770193
#3  0.014447  0.535801

2. Select Multiple Columns Using iloc() and loc()

  • iloc() for Integer-Based Indexing:
print(df.iloc[:,[0,2]])

#Output:
#          a         c
#0  0.255086  0.342223
#1  0.744271  0.861554
#2  0.420066  0.770193
#3  0.014447  0.535801

loc() for Label-Based Indexing:

print(df.loc[:,['a','c']])

#Output:
#          a         c
#0  0.255086  0.342223
#1  0.744271  0.861554
#2  0.420066  0.770193
#3  0.014447  0.535801

Conclusion

Efficiently selecting multiple columns in Pandas DataFrame empowers you to manipulate and analyze your data with precision. Whether you prefer the simplicity of __getitem__ syntax or the flexibility of iloc() and loc(), Pandas provides the tools you need to extract the exact columns you require. Remember, mastering these techniques is fundamental for effective data analysis in Python.

Use AI tools like ChatGPT and Gemini to learn coding efficiently!

You can also use AI tools like Gemini and ChatGPT to recreate the methods mentioned in the article and in more detail. It is free to register on these tools and you do not need any premium membership to use the prompts mentioned below.

select columns of pandas dataframe

Happy Learning!

Explore more from this category at Python DataFrames. Alternatively, search and view other topics at All Tutorials.