Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python Pandas – How to skip initial space from a DataFrame
When reading CSV files with Pandas, you may encounter data with unwanted leading whitespace in cells. The skipinitialspace parameter in pd.read_csv() automatically removes these leading spaces during import.
Understanding the skipinitialspace Parameter
The skipinitialspace parameter is set to False by default. When set to True, it removes whitespace immediately following the delimiter in CSV files.
Syntax
pd.read_csv(filepath, skipinitialspace=True)
Creating Sample CSV Data
Let's first create a CSV file with leading spaces to demonstrate the functionality ?
import pandas as pd
import io
# Create CSV data with leading spaces
csv_data = """Car, Place, UnitsSold
Audi, Bangalore, 80
Porsche, Mumbai, 110
RollsRoyce, Pune, 100
BMW, Delhi, 200
Mercedes, Hyderabad, 80"""
print("CSV data with leading spaces:")
print(csv_data)
CSV data with leading spaces: Car, Place, UnitsSold Audi, Bangalore, 80 Porsche, Mumbai, 110 RollsRoyce, Pune, 100 BMW, Delhi, 200 Mercedes, Hyderabad, 80
Reading CSV Without skipinitialspace
By default, read_csv() preserves the leading whitespace ?
import pandas as pd
import io
csv_data = """Car, Place, UnitsSold
Audi, Bangalore, 80
Porsche, Mumbai, 110
RollsRoyce, Pune, 100
BMW, Delhi, 200
Mercedes, Hyderabad, 80"""
# Read CSV without removing initial spaces
df_with_spaces = pd.read_csv(io.StringIO(csv_data))
print("DataFrame with leading spaces:")
print(df_with_spaces)
DataFrame with leading spaces:
Car Place UnitsSold
0 Audi Bangalore 80
1 Porsche Mumbai 110
2 RollsRoyce Pune 100
3 BMW Delhi 200
4 Mercedes Hyderabad 80
Reading CSV With skipinitialspace=True
Now let's remove the leading spaces by setting skipinitialspace=True ?
import pandas as pd
import io
csv_data = """Car, Place, UnitsSold
Audi, Bangalore, 80
Porsche, Mumbai, 110
RollsRoyce, Pune, 100
BMW, Delhi, 200
Mercedes, Hyderabad, 80"""
# Read CSV removing initial spaces
df_clean = pd.read_csv(io.StringIO(csv_data), skipinitialspace=True)
print("DataFrame with spaces removed:")
print(df_clean)
DataFrame with spaces removed:
Car Place UnitsSold
0 Audi Bangalore 80
1 Porsche Mumbai 110
2 RollsRoyce Pune 100
3 BMW Delhi 200
4 Mercedes Hyderabad 80
Comparison
| Parameter | Leading Spaces | Best For |
|---|---|---|
skipinitialspace=False (default) |
Preserved | When spaces are part of data |
skipinitialspace=True |
Removed | Clean data import |
Real-World Example
Here's how you would use it with an actual CSV file ?
import pandas as pd
# Reading CSV file with leading spaces removed
df = pd.read_csv("/path/to/your/file.csv", skipinitialspace=True)
print(df)
Conclusion
Use skipinitialspace=True when importing CSV files to automatically remove unwanted leading whitespace. This parameter ensures cleaner data without manual preprocessing steps.
