How do I use the "axis" parameter in pandas?
Key Takeaways
The video explains how to use the axis parameter in pandas, a Python library, to perform operations such as dropping columns or rows, and calculating means, by specifying the axis as 0 for rows or 1 for columns.
Full Transcript
hello and welcome back to my Q&A video series about the panda library in Python and the question for today is how do I use the axis parameter in pandas all right this is a bit of a technical topic uh we've seen the axis parameter once before in this series in the context of dropping rows and columns so we're going to start uh with a recap of that we're going to talk about the AIS parameter and then we're going to use it in another context okay so uh as always we'll start by importing pandas import pandas as PD and then we're going to read in a data set and this is going to be a data set of alcohol consumption by country so pd. read CSV and then pass it the URL bit. Le SL drinks by my country okay so we'll run that and then take a look at the first five rows and what we see is that each row represents a country and uh their reported alcohol consumption per adult okay so uh let's say we want to remove a column let's say we don't care about the continent column we would what we would do is use the uh drop method it's a data frame method so drinks. drop and then we pass it as a string the name of the column we want to drop and then we say axis equals one okay and uh I'm just going to go ahead and throw on the head method so that we're only looking at the first five rows of the resulting data frame now what you can see is that the continent column was indeed dropped okay and I had to specify axis equals 1 to say I want to drop a column okay now uh this did not I did not use the in place parameter um so this did not actually remove the column this is just a temporary thing okay so let's say instead I wanted to drop a row so I wanted to drop row two for example this is the index and uh I'll so instead I say drop two and I change it to axis equals z and this time you can see the continent uh column is back but this time row two is now gone okay so uh what exactly is going on here so uh pandas could have created a drop columns method that was specific to drop dropping columns and it could have created a separate drop rows method for dropping rows but instead the panda philosophy is to make a single method called drop and then we use the axis parameter to specify which what we want to drop do I want to drop a row or a column so axis one is the column axis axis zero is the row axis okay so uh anytime you run across a pandas command uh and you think it could refer to rows or columns that's how you'll specify which you want to refer to okay now let's try using axis in a different parameter um so what we're going to do is a mathematical operation a mean and let's just say I say drinks. mean what's going to happen so I'll run that and what we're seeing is that pandas has told us the mean of each of the numeric columns this is the mean value of the beer servings series of the spirit serving series wine servings and total leaders of pure alcohol okay so why did it give us the mean values for each series um great question so basically this is doing column means and that's because the default behavior of mean is Axis equals z so I'm just making it explicit this will have the same result I'm just making it explicit that when I said drinks. mean it was assuming the zero axis and in other words axis zero for an operation is saying I want you to move down I want you to move across the row axis this is the row axis and the direction I want the operation to occur is down so the way I like to imagine it is that these four columns the numeric columns are kind of being collapsed down into a single set of four numbers that represents the mean of each column okay so if instead you wanted the mean of each row you could say axis equals 1 and now you can picture that operation moving the mean operation moving along the column axis the one one axis it's moving this way and that way what it's doing is it's averaging it's taking the mean of these zeros which is zero it's taking the mean of these which is 69 etc etc now this is not a uh meaningful number because these are different units these are servings this is leaders so they're different units so this isn't like a meaningful number but um I just wanted to show you the principle of how to use the a of0 or one okay so uh just to reinforce this when you're moving along axis one you're preserving all the rows and there's 193 rows which is why uh that result has a shape of 193 whereas when we did axis equals z we got a shape of four because it was collapsing all the columns down into one number so again I think of the axis for an OP a mathematical operation as like the direction of the movement of the operation okay all right so as always we're going to end with a bonus tip and one one thing I want to show you is that you can actually they're actually aliases for the axis numbers so if you find zero you might find this more intuitive than axis Z and one um so if you want to say axis zero you can actually say axis equals index as a string and you'll get the same thing as axis equals z okay axis equals 1 which again would have given us 193 numbers this way you can say axis equals columns okay so you decide which is more intuitive um up to you I tend to use zero and one um but index and columns is also valid in pandas all right so that's it for today thanks so much for joining me uh feel free to click subscribe if you'd like to see more videos like this if you have a pandas tip or a question please let me know in the comment section below so that we can all help each other learn and um I hope to see you again soon thanks again
Original Description
When performing operations on a pandas DataFrame, such as dropping columns or calculating row means, it is often necessary to specify the "axis". But what exactly is an axis? In this video, I'll help you to build a mental model for understanding the axis parameter so that you will know when and how to use it.
SUBSCRIBE to learn data science with Python:
https://www.youtube.com/dataschool?sub_confirmation=1
JOIN the "Data School Insiders" community and receive exclusive rewards:
https://www.patreon.com/dataschool
== RESOURCES ==
GitHub repository for the series: https://github.com/justmarkham/pandas-videos
"drop" documentation: http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.drop.html
"mean" documentation: http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.mean.html
== LET'S CONNECT! ==
Newsletter: https://www.dataschool.io/subscribe/
Twitter: https://twitter.com/justmarkham
Facebook: https://www.facebook.com/DataScienceSchool/
LinkedIn: https://www.linkedin.com/in/justmarkham/
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from Data School · Data School · 32 of 60
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
▶
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Setting up Git and GitHub
Data School
Navigating a GitHub Repository - Part 1
Data School
Forking a GitHub Repository
Data School
Creating a New GitHub Repository
Data School
Copying a GitHub Repository to Your Local Computer
Data School
Committing Changes in Git and Pushing to a GitHub Repository
Data School
Syncing Your GitHub Fork
Data School
Allstate Purchase Prediction Challenge on Kaggle
Data School
Troubleshooting: Updates Rejected When Pushing to GitHub
Data School
Hands-on dplyr tutorial for faster data manipulation in R
Data School
ROC Curves and Area Under the Curve (AUC) Explained
Data School
Going deeper with dplyr: New features in 0.3 and 0.4 (tutorial)
Data School
What is machine learning, and how does it work?
Data School
Setting up Python for machine learning: scikit-learn and Jupyter Notebook
Data School
Getting started in scikit-learn with the famous iris dataset
Data School
Training a machine learning model with scikit-learn
Data School
Comparing machine learning models in scikit-learn
Data School
Data science in Python: pandas, seaborn, scikit-learn
Data School
Selecting the best model in scikit-learn using cross-validation
Data School
How to find the best model parameters in scikit-learn
Data School
How to evaluate a classifier in scikit-learn
Data School
What is pandas? (Introduction to the Q&A series)
Data School
How do I read a tabular data file into pandas?
Data School
How do I select a pandas Series from a DataFrame?
Data School
Why do some pandas commands end with parentheses (and others don't)?
Data School
How do I rename columns in a pandas DataFrame?
Data School
How do I remove columns from a pandas DataFrame?
Data School
How do I sort a pandas DataFrame or a Series?
Data School
How do I filter rows of a pandas DataFrame by column value?
Data School
How do I apply multiple filter criteria to a pandas DataFrame?
Data School
Your pandas questions answered!
Data School
How do I use the "axis" parameter in pandas?
Data School
How do I use string methods in pandas?
Data School
How do I change the data type of a pandas Series?
Data School
When should I use a "groupby" in pandas?
Data School
How do I explore a pandas Series?
Data School
How do I handle missing values in pandas?
Data School
What do I need to know about the pandas index? (Part 1)
Data School
What do I need to know about the pandas index? (Part 2)
Data School
How do I select multiple rows and columns from a pandas DataFrame?
Data School
Machine Learning with Text in scikit-learn (PyCon 2016)
Data School
When should I use the "inplace" parameter in pandas?
Data School
How do I make my pandas DataFrame smaller and faster?
Data School
How do I use pandas with scikit-learn to create Kaggle submissions?
Data School
More of your pandas questions answered!
Data School
How do I create dummy variables in pandas?
Data School
How do I work with dates and times in pandas?
Data School
How do I find and remove duplicate rows in pandas?
Data School
How do I avoid a SettingWithCopyWarning in pandas?
Data School
How do I change display options in pandas?
Data School
How do I create a pandas DataFrame from another object?
Data School
How do I apply a function to a pandas Series or DataFrame?
Data School
Getting started with machine learning in Python (webcast)
Data School
Q&A about Machine Learning with Text (online course)
Data School
Your pandas questions answered! (webcast)
Data School
Machine Learning with Text in scikit-learn (PyData DC 2016)
Data School
Write Pythonic Code for Better Data Science (webcast)
Data School
Web scraping in Python (Part 1): Getting started
Data School
Web scraping in Python (Part 2): Parsing HTML with Beautiful Soup
Data School
Web scraping in Python (Part 3): Building a dataset
Data School
More on: Data Literacy
View skill →Related AI Lessons
⚡
⚡
⚡
⚡
The AI Moat Paradox: The Better Models Become, the Less Models Matter
Medium · AI
170,927 AI Papers Reveal the Biggest Research Shifts of the First Half of 2026
Medium · Machine Learning
170,927 AI Papers Reveal the Biggest Research Shifts of the First Half of 2026
Medium · Data Science
[PoV] When Everyone Is Smart, No One Is
Medium · AI
🎓
Tutor Explanation
DeepCamp AI