Last updated on March 19th, 2025 at 05:54 am
Python developers commonly use Pandas dataframe to store, manipulate and analyze data. DataFrame allows you to store data in a tabular manner, as columns and rows. Often Python programmers need to select one or more columns from Pandas Dataframe. There are several ways to do this. In this article, we will learn how to select multiple columns in Pandas DataFrame.
How to Select Multiple Columns in Pandas DataFrame
Let us say you have the following Dataframe in Python.
import pandas as pd
data = {'id':[1,2,3,4],
'Age':[27, 24, 22, 32],
'Name':['John', 'Jane', 'Jim', 'Kim']}
df = pd.DataFrame(data)
print(df)
## Output
id Age Name
0 1 27 John
1 2 24 Jane
2 3 22 Jim
3 4 32 Kim
Let us look at the different ways to select only columns id and name from the above dataframe.
1. Using Basic Method
You can select multiple columns from a dataframe by passing a list of column names that you want, in square brackets […] as shown.
df1 = df[['id','Name']]
print(df1)
## Output
id Name
0 1 John
1 2 Jane
2 3 Jim
3 4 Kim
Please use the correct case for each column name, otherwise you will get an error. This method is very useful if you already know the column names, or you want to select columns that are not contiguous.
Also, in this method, a new copy of dataframe with the required columns instead of creating a view. This means the dataframe df1 will occupy a different memory space, and any changes to it will not affect the original dataframe, and vice versa.
If you need to select a lot of contiguous columns, then entering their names one by one may be tedious. In this case, you can directly use df.columns[…] to retrieve a list of column names that you want to select in the result. Here is an example to select 1st and 2nd columns, that is, columns first to second.
df1 = df[df.columns[0:2]]
print(df1)
## Output
id Age
0 1 27
1 2 24
2 3 22
3 4 32
This method is useful if you want to select many contiguous columns, and/or if you do not know their names.
2. Using loc
Every dataframe also supports loc function that allows you to select specific rows and columns, as per your requirement. Here is its syntax.
dataframe.loc[from_row:to_row,from_column:to_column]
OR
dataframe.loc[from_row:to_row,list_of_column_names]
Here is an example to select columns id and Age columns from our dataframe.
df1 = df.loc[:,'id':'Age']
OR
df1 = df.loc[:,['id','Age']]
print(df1)
## output
id Age
0 1 27
1 2 24
2 3 22
3 4 32
This is method is useful if you want to select multiple columns that are contiguous and you know the column names.
3. Using iloc
Similarly, every dataframe also supports iloc function that allows you to select one or more columns using their indexes instead of their names. Here is its syntax.
dataframe.iloc[from_row:to_row,from_column:to_column]
OR
dataframe.iloc[from_row:to_row,list_of_column_indexes]
Here is an example to select columns age and name using iloc.
df1 = df.iloc[:,1:3]
OR
df1 = df.iloc[:,[1,2,3]]
print(df1)
## Output
Age Name
0 27 John
1 24 Jane
2 22 Jim
3 32 Kim
Please note, in this case, iloc function will return a view of the dataframe. In other words, the new dataframe variable df1 will continue to refer to the same memory chunk as original dataframe df. It will just be accessible as a sub object. Any change in the view will affect the original dataframe and vice versa.
4. Using ix
If you are using Pandas < 0.20, then you can use ix function instead of iloc. It has the same syntax as iloc and even works the same way. It allows you to select specific rows and columns from a dataframe, using their indexes. Here is an example to select ‘Age’ and ‘Name’ columns from dataframe using ix function.
df1 = df.iloc[:,1:3]
print(df1)
## Output
Age Name
0 27 John
1 24 Jane
2 22 Jim
3 32 Kim
If you call this function in Pandas >0.2, then it will give you an error saying dataframe has no attribute ix.
Please note
When selecting one or more columns from Dataframes, it is important to know whether the approach results in a view or a copy. A copy occupies a different memory space compared to the original dataframe. It is not affected when the original dataframe changes and vice versa. On the other hand, a view will continue to refer to the same memory space, as a sub object or slice. So any changes in the view will affect the original dataframe and vice versa.
Conclusion
In this article, we have learnt several different ways to select multiple columns from Python dataframes. If you want to select non-contiguous columns and you know their names, then you can use the basic method for this purpose. In this case, you can select columns by simply supplying a list of column names. If you want to select many contiguous columns using their indexes, then you can use loc or iloc function. This is also useful if you want to select many columns using compact code. You can use any of these solutions as per your requirement.
Also read:
How to Randomly Select Item from Python List
How to Delete Column in Pandas DataFrame
How to Sort Python Dictionary By Value

Sreeram Sreenivasan is the Founder of Ubiq. He has helped many Fortune 500 companies in the areas of BI & software development.