
Problem –
You have a pandas dataframe and you want to select multiple columns from it and may be save it in a new dataframe.
Solution –
There are multiple ways to select multiple columns from a pandas dataframe. Let’s read a dataset first for illustration.
# import pandas
import pandas as pd
#read data
url="https://raw.githubusercontent.com/bprasad26/lwd/master/data/clothing_store_sales.csv"
df = pd.read_csv(url)
df.head()

Using Bracket Notation –
To select multiple columns from a pandas dataframe we can use the bracket notation. To do that all you have to do is pass the name of the columns in a list like this.
# select Gender and Marital Status
df[['Gender','Marital Status']]

Using loc method –
The loc method in pandas is used to select rows and columns based on labels or names.
The general syntax of the loc method is –
df.loc[[rows], [columns]]
The part to the left of the comma is the rows that you want to select and the part to the right of the comma are the columns that you want to select.
To select multiple columns using loc method, you can pass names of columns in a list to the loc method.
# using loc select Gender and Marital Status
df.loc[:, ['Gender','Marital Status']]

Using iloc method –
The iloc method is similar to the loc method but instead of using labels or names, the iloc method uses the row or column numbers. And pandas uses 0 based index, so for example if you want to select the first column then you will write 0 not 1.
To select multiple columns using iloc you have to write.
# select Gender and Marital Status using iloc
df.iloc[:, [5, 6]]
