How do I use the "axis" parameter in pandas?

Data School · Intermediate ·📰 AI News & Updates ·10y ago

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 Setting up Git and GitHub
Setting up Git and GitHub
Data School
2 Navigating a GitHub Repository - Part 1
Navigating a GitHub Repository - Part 1
Data School
3 Forking a GitHub Repository
Forking a GitHub Repository
Data School
4 Creating a New GitHub Repository
Creating a New GitHub Repository
Data School
5 Copying a GitHub Repository to Your Local Computer
Copying a GitHub Repository to Your Local Computer
Data School
6 Committing Changes in Git and Pushing to a GitHub Repository
Committing Changes in Git and Pushing to a GitHub Repository
Data School
7 Syncing Your GitHub Fork
Syncing Your GitHub Fork
Data School
8 Allstate Purchase Prediction Challenge on Kaggle
Allstate Purchase Prediction Challenge on Kaggle
Data School
9 Troubleshooting: Updates Rejected When Pushing to GitHub
Troubleshooting: Updates Rejected When Pushing to GitHub
Data School
10 Hands-on dplyr tutorial for faster data manipulation in R
Hands-on dplyr tutorial for faster data manipulation in R
Data School
11 ROC Curves and Area Under the Curve (AUC) Explained
ROC Curves and Area Under the Curve (AUC) Explained
Data School
12 Going deeper with dplyr: New features in 0.3 and 0.4 (tutorial)
Going deeper with dplyr: New features in 0.3 and 0.4 (tutorial)
Data School
13 What is machine learning, and how does it work?
What is machine learning, and how does it work?
Data School
14 Setting up Python for machine learning: scikit-learn and Jupyter Notebook
Setting up Python for machine learning: scikit-learn and Jupyter Notebook
Data School
15 Getting started in scikit-learn with the famous iris dataset
Getting started in scikit-learn with the famous iris dataset
Data School
16 Training a machine learning model with scikit-learn
Training a machine learning model with scikit-learn
Data School
17 Comparing machine learning models in scikit-learn
Comparing machine learning models in scikit-learn
Data School
18 Data science in Python: pandas, seaborn, scikit-learn
Data science in Python: pandas, seaborn, scikit-learn
Data School
19 Selecting the best model in scikit-learn using cross-validation
Selecting the best model in scikit-learn using cross-validation
Data School
20 How to find the best model parameters in scikit-learn
How to find the best model parameters in scikit-learn
Data School
21 How to evaluate a classifier in scikit-learn
How to evaluate a classifier in scikit-learn
Data School
22 What is pandas? (Introduction to the Q&A series)
What is pandas? (Introduction to the Q&A series)
Data School
23 How do I read a tabular data file into pandas?
How do I read a tabular data file into pandas?
Data School
24 How do I select a pandas Series from a DataFrame?
How do I select a pandas Series from a DataFrame?
Data School
25 Why do some pandas commands end with parentheses (and others don't)?
Why do some pandas commands end with parentheses (and others don't)?
Data School
26 How do I rename columns in a pandas DataFrame?
How do I rename columns in a pandas DataFrame?
Data School
27 How do I remove columns from a pandas DataFrame?
How do I remove columns from a pandas DataFrame?
Data School
28 How do I sort a pandas DataFrame or a Series?
How do I sort a pandas DataFrame or a Series?
Data School
29 How do I filter rows of a pandas DataFrame by column value?
How do I filter rows of a pandas DataFrame by column value?
Data School
30 How do I apply multiple filter criteria to a pandas DataFrame?
How do I apply multiple filter criteria to a pandas DataFrame?
Data School
31 Your pandas questions answered!
Your pandas questions answered!
Data School
How do I use the "axis" parameter in pandas?
How do I use the "axis" parameter in pandas?
Data School
33 How do I use string methods in pandas?
How do I use string methods in pandas?
Data School
34 How do I change the data type of a pandas Series?
How do I change the data type of a pandas Series?
Data School
35 When should I use a "groupby" in pandas?
When should I use a "groupby" in pandas?
Data School
36 How do I explore a pandas Series?
How do I explore a pandas Series?
Data School
37 How do I handle missing values in pandas?
How do I handle missing values in pandas?
Data School
38 What do I need to know about the pandas index? (Part 1)
What do I need to know about the pandas index? (Part 1)
Data School
39 What do I need to know about the pandas index? (Part 2)
What do I need to know about the pandas index? (Part 2)
Data School
40 How do I select multiple rows and columns from a pandas DataFrame?
How do I select multiple rows and columns from a pandas DataFrame?
Data School
41 Machine Learning with Text in scikit-learn (PyCon 2016)
Machine Learning with Text in scikit-learn (PyCon 2016)
Data School
42 When should I use the "inplace" parameter in pandas?
When should I use the "inplace" parameter in pandas?
Data School
43 How do I make my pandas DataFrame smaller and faster?
How do I make my pandas DataFrame smaller and faster?
Data School
44 How do I use pandas with scikit-learn to create Kaggle submissions?
How do I use pandas with scikit-learn to create Kaggle submissions?
Data School
45 More of your pandas questions answered!
More of your pandas questions answered!
Data School
46 How do I create dummy variables in pandas?
How do I create dummy variables in pandas?
Data School
47 How do I work with dates and times in pandas?
How do I work with dates and times in pandas?
Data School
48 How do I find and remove duplicate rows in pandas?
How do I find and remove duplicate rows in pandas?
Data School
49 How do I avoid a SettingWithCopyWarning in pandas?
How do I avoid a SettingWithCopyWarning in pandas?
Data School
50 How do I change display options in pandas?
How do I change display options in pandas?
Data School
51 How do I create a pandas DataFrame from another object?
How do I create a pandas DataFrame from another object?
Data School
52 How do I apply a function to a pandas Series or DataFrame?
How do I apply a function to a pandas Series or DataFrame?
Data School
53 Getting started with machine learning in Python (webcast)
Getting started with machine learning in Python (webcast)
Data School
54 Q&A about Machine Learning with Text (online course)
Q&A about Machine Learning with Text (online course)
Data School
55 Your pandas questions answered! (webcast)
Your pandas questions answered! (webcast)
Data School
56 Machine Learning with Text in scikit-learn (PyData DC 2016)
Machine Learning with Text in scikit-learn (PyData DC 2016)
Data School
57 Write Pythonic Code for Better Data Science (webcast)
Write Pythonic Code for Better Data Science (webcast)
Data School
58 Web scraping in Python (Part 1): Getting started
Web scraping in Python (Part 1): Getting started
Data School
59 Web scraping in Python (Part 2): Parsing HTML with Beautiful Soup
Web scraping in Python (Part 2): Parsing HTML with Beautiful Soup
Data School
60 Web scraping in Python (Part 3): Building a dataset
Web scraping in Python (Part 3): Building a dataset
Data School

The video teaches how to use the axis parameter in pandas to perform various operations, such as dropping columns or rows, and calculating means, by specifying the axis as 0 for rows or 1 for columns.

Key Takeaways
  1. Import pandas library
  2. Create a data frame
  3. Drop a column using axis=1
  4. Drop a row using axis=0
  5. Calculate mean using axis=0 or axis=1
💡 The axis parameter in pandas is used to specify the direction of the operation, with axis 0 referring to rows and axis 1 referring to columns.

Related Reads

📰
Claude Sonnet 5 Didn’t Just Get Smarter. It Changed the Economics of AI.
Learn how Claude Sonnet 5's advancements changed the economics of AI, making 'good enough AI' viable for production, and understand the implications for AI development and deployment
Medium · AI
📰
Claude Sonnet 5 Didn’t Just Get Smarter. It Changed the Economics of AI.
Claude Sonnet 5's improved AI capabilities have transformed the economics of AI, making it more viable for production
Medium · Machine Learning
📰
The AI Career Toolkit That Replaced My Job Hunt in 2026
Learn how to leverage AI tools to enhance your job search and career development in 2026
Dev.to · freelancewith_ai
📰
The AI Problem Nobody Saw Coming: The Decline Of Curiosity And Meaning
The rise of AI may lead to a decline in human curiosity, affecting innovation and our sense of meaning, and it's crucial to understand this potential consequence
Forbes Innovation
Up next
Man dies after horror Gold Coast house fire; high-speed Sydney motorway pursuit | 9 News Australia
9 News Australia
Watch →