There are at least 3 methods to select 2 or more than 2 columns from a dataframe in Python.
Method 1: Use a list of column names
df[ [ "col1", "col2" ] ]
Method 2: Use a range of column index
df.iloc [ : , 0 : 2 ]
Method 3: Use specific column indexes
df.iloc [ : , [ 0, 1] ]
Example for method 1
The following uses df[["col1", "col2"]] to select two columns from a Pandas dataframe.
import pandas as pd
# Create a dataframe
car_data = {'Brand': ['Tesla', 'Tesla','Tesla','Ford','Ford'],
'Location': ['CA', 'CA','NY','MA','CA'],
'Year':['2019','2018','2020','2019','2019']}
car_data=pd.DataFrame(data=car_data)
#print out the original dataframe
print('Original Dataframe: \n', car_data)
# subset first two columns
selected_columns=car_data[['Brand','Location']]
# print out the selected columns
print('Selected columns: \n', selected_columns)
The following is the output, showing the original dataframe and the selected two columns.
Original Dataframe:
Brand Location Year
0 Tesla CA 2019
1 Tesla CA 2018
2 Tesla NY 2020
3 Ford MA 2019
4 Ford CA 2019
Selected columns:
Brand Location
0 Tesla CA
1 Tesla CA
2 Tesla NY
3 Ford MA
4 Ford CA
Example for method 2
The following uses df.iloc[[:,0:2]] to select two columns from a Pandas dataframe.
import pandas as pd
# Create a dataframe
car_data = {'Brand': ['Tesla', 'Tesla','Tesla','Ford','Ford'],
'Location': ['CA', 'CA','NY','MA','CA'],
'Year':['2019','2018','2020','2019','2019']}
car_data=pd.DataFrame(data=car_data)
#print out the original dataframe
print('Original Dataframe: \n', car_data)
# subset first two columns using iloc[]
selected_columns=car_data.iloc[:,0:2]
# print out the selected columns
print('Selected columns: \n', selected_columns)
The following is the output, showing the original dataframe and the selected two columns using iloc[].
Original Dataframe:
Brand Location Year
0 Tesla CA 2019
1 Tesla CA 2018
2 Tesla NY 2020
3 Ford MA 2019
4 Ford CA 2019
Selected columns:
Brand Location
0 Tesla CA
1 Tesla CA
2 Tesla NY
3 Ford MA
4 Ford CA
Example of method 3
The following uses df.iloc[[:,[0,1]]] to select two columns in Python.
import pandas as pd
# Create a dataframe
car_data = {'Brand': ['Tesla', 'Tesla','Tesla','Ford','Ford'],
'Location': ['CA', 'CA','NY','MA','CA'],
'Year':['2019','2018','2020','2019','2019']}
car_data=pd.DataFrame(data=car_data)
#print out the original dataframe
print('Original Dataframe: \n', car_data)
# subset first two columns using iloc[]
selected_columns=car_data.iloc[:,[0,1]]
# print out the selected columns
print('Selected two columns: \n', selected_columns)
The following shows the original dataframe and the selected two columns.
Original Dataframe:
Brand Location Year
0 Tesla CA 2019
1 Tesla CA 2018
2 Tesla NY 2020
3 Ford MA 2019
4 Ford CA 2019
Selected two columns:
Brand Location
0 Tesla CA
1 Tesla CA
2 Tesla NY
3 Ford MA
4 Ford CA