In this tutorial, we will look at how to remove spaces from the column names of a pandas dataframe with the help of some examples.
How to remove spaces from pandas column names?
You can apply the string replace()
function to remove spaces from column names of a pandas dataframe. The idea is the replace the space character, ' '
with an empty string, ''
.
You can use the .str
accessor to apply string functions to all the column names in a pandas dataframe. Use the following syntax to remove spaces from column names –
# remove spaces from column names df.columns = df.columns.str.replace(" ", "")
Here, we are essentially removing the spaces from column names by replacing them with an empty string. You can similarly replace spaces with any other character, for example, an underscore, '_'
.
Examples
Let’s now look at some examples of using the above syntax.
First, we will create a pandas dataframe that we will be using throughout this tutorial.
import pandas as pd # employee data data = { "First Name": ["Jim", "Dwight", "Angela", "Tobi"], "Last Name": ["Halpert", "Schrute", "Martin", "Flenderson"], "Age": [26, 28, 27, 32] } # create pandas dataframe df = pd.DataFrame(data) # display the dataframe df
Output:
Introductory ⭐
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
Intermediate ⭐⭐⭐
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
Advanced ⭐⭐⭐⭐⭐
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 Find Data Science Programs 👨💻 111,889 already enrolled
Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.
Here, we created a dataframe with information about some employees in an office. The dataframe has the columns – “First Name”, “Last Name”, and “Age”. You can see that two of the column names have spaces in them.
Example 1 – Remove spaces from all the column names
Let’s remove the spaces from all the column names by replacing them with an empty string character.
# remove spaces from column names df.columns = df.columns.str.replace(" ", "") # display the dataframe df
Output:
You can see that the column names now do not have any spaces in them.
Example 2 – Replace spaces in column names with underscore
Instead of replacing spaces with an empty string, we can replace them with a specific string, for example, an underscore '_'
character.
# reset column names to original names df.columns = ["First Name", "Last Name", "Age"] # replace spaces with underscores in column names df.columns = df.columns.str.replace(" ", "_") # display the dataframe df
Output:
The column names now have underscores in place of spaces.
Summary
In this tutorial, we looked at how to remove spaces from the column names of a dataframe. The following are the key takeaways –
- Use the string
replace()
function (applied using the.str
accessor ondf.columns
) to remove spaces from column names by replacing them with an empty string. - You can also replace spaces with custom strings, for example, an underscore character,
'_'
using thereplace()
function.
You might also be interested in –
- Pandas – Change Column Names to Uppercase
- Pandas – Change Column Names to Lowercase
- Remove Prefix or Suffix from Pandas Column Names
- Get Column Names as List in Pandas DataFrame
- Pandas – Rename Column Names
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.