Use DataFrame.loc[] and DataFrame.iloc[] to select a single column or multiple columns from pandas DataFrame by column names/label or index position respectively. where loc[] is used with column labels/names and iloc[] is used with column index/position. You can also use these operators to select rows from Pandas DataFrame. Also, refer to a related article how to get cell value from pandas DataFrame.
Pandas DataFrame is a two-dimensional tabular data structure with labeled axes. i.e. columns and rows. Selecting columns from DataFrame results in a new DataFrame containing only specified selected columns from the original DataFrame.
In this article, I will explain how to select single or multiple columns from DataFrame by column labels & index, certain positions of the column, and by range e.t.c with examples.
1. Quick Examples of Selecting Columns from Pandas DataFrame
If you are in a hurry, below are some quick examples of how to select single or multiple columns from Pandas DataFrame by column name and index.
# Below are some quick examples
# Example 1: By using df[] Notation
df2 = df[["Courses","Fee","Duration"]] # select multile columns
# Example 2: Using loc[] to take column slices
df2 = df.loc[:, ["Courses","Fee","Duration"]] # Selecte multiple columns
df2 = df.loc[:, ["Courses","Fee","Discount"]] # Select Random columns
df2 = df.loc[:,'Fee':'Discount'] # Select columns between two columns
df2 = df.loc[:,'Duration':] # Select columns by range
df2 = df.loc[:,:'Duration'] # Select columns by range
df2 = df.loc[:,::2] # Select every alternate column
# Example 3: Using iloc[] to select column by Index
df2 = df.iloc[:,[1,3,4]] # Select columns by Index
df2 = df.iloc[:,1:4] # Select between indexes 1 and 4 (2,3,4)
df2 = df.iloc[:,2:] # Select From 3rd to end
df2 = df.iloc[:,:2] # Select First Two Columns
Now, let’s create a DataFrame with a few rows and columns and execute some examples of how to select columns in pandas. Our DataFrame contains column names Courses
, Fee
, Duration
, and Discount
.
import pandas as pd
technologies = {
'Courses':["Spark","PySpark"],
'Fee' :[20000,25000],
'Duration':['30days','40days'],
'Discount':[1000,2300]
}
df = pd.DataFrame(technologies)
print("Create DataFrame:\n", df)
Yields below output.
2. Using loc[] to Select Columns by Name
By using pandas.DataFrame.loc[] you can select columns by names or labels. To select the columns by name, the syntax is df.loc[:,start:stop:step]
; where start
is the name of the first column to take, stop
is the name of the last column to take, and step
as the number of indices to advance after each extraction; for example, you can select alternate columns to use the syntax: [:,[labels]]
with labels as a list of column names to take.
# loc[] syntax to slice columns
df.loc[:,start:stop:step]
2.1 Select DataFrame Columns by Name
To select single or multiple columns by labels or names, all you need is to provide the names of the columns as a list. Here we use the []
notation instead of df.loc[,start:stop:step]
approach.
# Select Columns by labels
df2 = df[["Courses","Fee","Duration"]]
print("Select columns by labels:\n", df2)
Yields below output.
2.2 Select Multiple Columns
Sometimes you may want to select multiple columns from Pandas DataFrame, you can do this by passing multiple column names/labels as a list. Note that loc[] also supports multiple conditions when selecting rows based on column values.
# Select Multiple Columns
df2 = df.loc[:, ["Courses","Fee","Discount"]]
print("Select multiple columns by labels:\n", df2)
# Output:
# Select multiple columns by labels:
# Courses Fee Discount
# 0 Spark 20000 1000
# 1 PySpark 25000 2300
2.3 Select DataFrame Columns by Range
When you want to select columns by the range, provide start and stop column names.
- By not providing a start column, loc[] selects from the beginning.
- By not providing stop, loc[] selects all columns from the start label.
- Providing both start and stop, selects all columns in between.
# Select all columns between Fee an Discount columns
df2 = df.loc[:,'Fee':'Discount']
print("Select columns by labels:\n", df2)
# Output
# Select columns by labels:
# Fee Duration Discount
# 0 20000 30days 1000
# 1 25000 40days 2300
# Select from 'Duration' column
df2 = df.loc[:,'Duration':]
print("Select columns by labels:\n", df2)
# Output
# Select columns by labels:
# Duration Discount Tutor
# 0 30days 1000 Michel
# 1 40days 2300 Sam
# Select from beginning and end at 'Duration' column
df2 = df.loc[:,:'Duration']
print("Select columns by labels:\n", df2)
# Output
# Select columns by labels:
# Courses Fee Duration
# 0 Spark 20000 30days
# 1 PySpark 25000 40days
2.4 Select Every Alternate Column
Using loc[]
, you can also select every other column from Pandas DataFrame.
# Select every alternate column
df2 = df.loc[:,::2]
print("Select columns by labels:\n", df2)
# Output:
# Select columns by labels:
# Courses Duration Tutor
# 0 Spark 30days Michel
# 1 PySpark 40days Sam
3. Pandas iloc[] to Select Column by Index or Position
By using pandas.DataFrame.iloc[] you can select columns from DataFrame by position/index, remember index starts from 0. You can use iloc[]
with the syntax [:,start:stop:step]
where start
indicates the index of the first column to take, stop
indicates the index of the last column to take, and step
indicates the number of indices to advance after each extraction. Or, use the syntax: [:,[indices]]
with indices as a list of column indices to take.
3.1. Select Multiple Columns by Index Position
Below example retrieves "Fee"
,"Discount"
and "Duration"
and returns a new DataFrame with the columns selected.
# Select columns by position
df2 = df.iloc[:,[1,3,4]]
print("Selec columns by position:\n", df2)
# Output:
# Selec columns by position:
# Fee Discount Tutor
# 0 20000 1000 Michel
# 1 25000 2300 Sam
3.2 Select Columns by Position Range
You can also slice a DataFrame by a range of positions.
# Select between indexes 1 and 4 (2,3,4)
df2 = df.iloc[:,1:4]
print("Select columns by position:\n", df2)
# OUtput:
# Selec columns by position:
# Fee Duration Discount
# 0 20000 30days 1000
# 1 25000 40days 2300
# Select From 3rd to end
df2 = df.iloc[:,2:]
print("Select columns by position:\n", df2)
# Output:
# Selec columns by position:
# Duration Discount Tutor
# 0 30days 1000 Michel
# 1 40days 2300 Sam
# Select First Two Columns
df2 = df.iloc[:,:2]
print("Selec columns by position:\n", df2))
# Output:
# Selec columns by position:
# Courses Fee
# 0 Spark 20000
# 1 PySpark 25000
To get the last column use df.iloc[:,-1:]
and to get just the first column df.iloc[:,:1]
4. Complete Example of Pandas Select Columns
Below is a complete example of how to select columns from Pandas DataFrame.
import pandas as pd
technologies = {
'Courses':["Spark","PySpark"],
'Fee' :[20000,25000],
'Duration':['30days','40days'],
'Discount':[1000,2300],
'Tutor':['Michel','Sam']
}
df = pd.DataFrame(technologies)
print(df)
# Select multiple columns
print(df[["Courses","Fee","Duration"]])
# Select Random columns
print(df.loc[:, ["Courses","Fee","Discount"]])
# Select columns by range
print(df.loc[:,'Fee':'Discount'])
print(df.loc[:,'Duration':])
print(df.loc[:,:'Duration'])
# Select every alternate column
print(df.loc[:,::2])
# Selected by column position
print(df.iloc[:,[1,3,4]])
# Select between indexes 1 and 4 (2,3,4)
print(df.iloc[:,1:4])
# Select From 3rd to end
print(df.iloc[:,2:])
# Select First Two Columns
print(df.iloc[:,:2])
Frequently Asked Questions on Pandas Select Columns
To select a single column by name, you can use square bracket([]) or dot(.) notation. For example, df['column_name']
or df.column_name
To select multiple columns by name, you can pass a list of column names within square brackets. For example, df[['column_name1', 'column_name2']]
You can select columns by their index using the df.iloc[]
attribute. For example, df.iloc[:, [0, 2]]
Use to Select the first and third columns.
You can use the .loc
attribute to select a column by name and .iloc
to select by index. For example, df['column_name']
Use to select by name and df.iloc[:, 0]
uUse to select by index.
You can select all columns by using a colon :
in place of column names or indices. For example, df[:]
Use to select all columns.
Conclusion
In this article, you have learned how to select single or multiple columns from pandas DataFrame using DataFrame.loc[], and DataFrame.iloc[] properties. To understand the similarities and differences of these two refer to pandas loc[] vs iloc[].
Happy Learning !!
Related Articles
- How to Add an Empty Column to a Pandas DataFrame
- How to Combine Two Series into pandas DataFrame
- Install pandas on Windows Step-by-Step
- Convert Index to Column in Pandas DataFrame
- Replace NaN Values with Zeroes in a Column of a Pandas DataFrame
- Pandas Select DataFrame Rows Between Two Dates
- Pandas Select Rows Based on List Index
- Pandas Select Multiple Columns in DataFrame
- Pandas Select Columns by Name or Index