Skip to content
  • (+91) 9409548155
  • support@appdividend.com
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
Menu
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
Python

How to Remove Spaces from Column Names in Pandas DataFrame

  • 09 Jan, 2025
  • Com 0
Featured Image of Removing Spaces from Column Names in Pandas DataFrame

To remove spaces from column names in Pandas DataFrame, the most efficient way is to use the “.columns” attribute with the “str.replace()” method. It directly operates on the index of the columns (which is a string index) and performs string replacement.

import pandas as pd

# Sample DataFrame
data = {'Col 1': [1, 2, 3], 'Col 2': [
    4, 5, 6], 'Col 3': [7, 8, 9]}

df = pd.DataFrame(data)
print(df)

# Remove all spaces
df.columns = df.columns.str.replace(' ', '')
print(df)

Output

Removing Spaces from Column Names in Pandas DataFrame using .columns.str.replace()

You can see from the above figure that we removed space between column names.

Replacing spaces with underscores

We can also replace spaces with underscores if we have that kind of requirement. In that case, we must replace ” “ with “_”.

import pandas as pd

data = {'Col 1': [1, 2, 3], 'Col 2': [
    4, 5, 6], 'Col 3': [7, 8, 9]}

df = pd.DataFrame(data)
print(df)

# Replace space with underscore (_)
df.columns = df.columns.str.replace(' ', '_')
print(df)

Output

Replacing spaces with underscores

Remove leading/trailing spaces from column names

You can also remove leading/trailing spaces from column names using the “.strip()” method.

import pandas as pd

data = {' Col 1 ': [1, 2, 3], ' Col 2 ': [
    4, 5, 6], ' Col 3 ': [7, 8, 9]}

df = pd.DataFrame(data)
print(df)

# Remove leading and trailing whitespaces
df.columns = df.columns.str.strip()
print(df)

Output

Remove leading:trailing spaces from column namesThat’s all!

Post Views: 25
Share on:
Krunal Lathiya

With a career spanning over eight years in the field of Computer Science, Krunal’s expertise is rooted in a solid foundation of hands-on experience, complemented by a continuous pursuit of knowledge.

Printing DataFrame without an Index in Pandas
How to Change Column Names to Lowercase in Pandas DataFrame

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Address: TwinStar, South Block – 1202, 150 Ft Ring Road, Nr. Nana Mauva Circle, Rajkot(360005), Gujarat, India

Call: (+91) 9409548155

Email: support@appdividend.com

Online Platform

  • Pricing
  • Instructors
  • FAQ
  • Refund Policy
  • Support

Links

  • About Us
  • Contact Us
  • Privacy Policy
  • Terms of services

Tutorials

  • Angular
  • React
  • Python
  • Laravel
  • Javascript
Copyright @2024 AppDividend. All Rights Reserved
Appdividend