Member-only story
Selecting Multiple Columns From a Pandas DataFrame
Discussing how to select multiple columns from a DataFrame in pandas
Introduction
Multiple column selection is one of the most common and simple tasks one can perform. In today’s short guide we will discuss about a few possible ways for selecting multiple columns from a pandas DataFrame. Specifically, we will explore how to do so
- using basing indexing
- with
loc
- using
iloc
- through the creation of a new DataFrame
Additionally, we will discuss when to use one method over the other, based on your specific use-case and whether you need to generate a view or a copy of the original DataFrame object.
First, let’s create an example DataFrame that we’ll reference throughout this article in order to demonstrate a few concepts.
import pandas pd
df = pd.DataFrame({
'colA':[1, 2, 3],
'colB': ['a', 'b', 'c'],
'colC': [True, False, True],
'colD': [1.0, 2.0, 3.0],
})print(df)
colA colB colC colD
0 1 a True 1.0
1 2 b False 2.0
2 3 c True 3.0