Data Wrangling
Importing Data Select, Drop & Rename Filter, Sort & Sample Add Columns Cleaning Data Dates & Time Join Data Aggregate & Transform
Data Analysis
Exploring Data Plotting Continuous Variables Plotting Discrete Variables
Machine Learning
Data Preparation Linear Models
Other Tutorials & Content
Learn Python for Data Science Learn Alteryx Blog



Select, Drop and Rename with Pandas

Select Columns & Rows

Select Columns with a List

Selecting the Order_Date and ProductID columns from the orders data frame.

orders[['Order_Date', 'ProductID']]
View Our Profile on Datasnips.com to See Our Data Science Code Snippets

Select Columns Using Loc

Select all columns between Order_No and ProductID from the orders dataframe using Loc.

orders.loc[:,'Order_No':'ProductID']

Select Columns Using iLoc

Select the first five columns from the orders dataframe using iLoc.

orders.iloc[:,0:5]

Select Rows Using iLoc

Select the first ten rows from the orders dataframe using iLoc.

orders.iloc[0:10,:]

Select the Last N Rows Using iLoc

Select the last ten rows from the orders dataframe using iLoc.

orders.iloc[-10:,:]

Drop Columns

Drop the PromoID and Order_Date columns from the orders dataframe.

orders.drop(labels=['PromoID','Order_Date'], axis=1, inplace=True)

Rename Columns

Rename Columns with a Dictionary

Rename the "Order_Date" column of the orders data frame to "Date".

orders.rename(columns={'Order_Date' :'Date'},inplace=True)

Rename All Columns with a List

Rename all three columns in the df dataframe to col_1, col_2 and col_3 using a list.

df.columns = ['col_1','col_2','col_3']