How can I strip the whitespace from Pandas DataFrame headers?
You can give functions to the rename
method. The str.strip()
method should do what you want:
In [5]: df
Out[5]:
Year Month Value
0 1 2 3
[1 rows x 3 columns]
In [6]: df.rename(columns=lambda x: x.strip())
Out[6]:
Year Month Value
0 1 2 3
[1 rows x 3 columns]
Note: that this returns a DataFrame
object and it's shown as output on screen, but the changes are not actually set on your columns. To make the changes, either use this in a method chain or re-assign the df
variabe:
df = df.rename(columns=lambda x: x.strip())
How to remove whitespace from df column headers (strip isn't working)
Seems like you need replace
all ' '
to ''
df.columns.str.replace(' ','')
Out[103]:
Index(['NodeNumber', 'X[m]', 'Y[m]', 'Z[m]',
'TurbulenceKineticEnergy[m^2s^-2]', 'turbulenceIntensity',
'Velocityu[ms^-1]', 'Velocityv[ms^-1]', 'Velocityw[ms^-1]',
'windspeedratio'],
dtype='object')
How to strip white spaces in headers with Python and pandas
This should help to clean up the columns names:
df.columns = [x.strip() for x in df.columns]
Pandas - Strip white space
You can strip()
an entire Series in Pandas using .str.strip():
df1['employee_id'] = df1['employee_id'].str.strip()
df2['employee_id'] = df2['employee_id'].str.strip()
This will remove leading/trailing whitespaces on the employee_id
column in both df1
and df2
Alternatively, you can modify your read_csv
lines to also use skipinitialspace=True
df1 = pd.read_csv('input1.csv', sep=',\s+', delimiter=',', encoding="utf-8", skipinitialspace=True)
df2 = pd.read_csv('input2.csv', sep=',\s,', delimiter=',', encoding="utf-8", skipinitialspace=True)
It looks like you are attempting to remove spaces in a string containing numbers. You can do this by:
df1['employee_id'] = df1['employee_id'].str.replace(" ","")
df2['employee_id'] = df2['employee_id'].str.replace(" ","")
Remove all whitespace in the header of a dataframe in pandas
You can use rename:
df.rename(columns=lambda x: x.strip())
How to remove excess whitespaces in entire python dataframe columns
You could use apply
:
df = df.applymap(lambda x: " ".join(x.split()) if isinstance(x, str) else x)
Is there a way to trim/strip whitespace in multiple columns of a pandas dataframe?
Use DataFrame.apply
with list of columns:
cols = ['col_1', 'col_2', 'col_4']
df[cols] = df[cols].apply(lambda x: x.str.strip())
Or parse only object columns, it is obviously strings:
cols = df.select_dtypes(object).columns
df[cols] = df[cols].apply(lambda x: x.str.strip())
Related Topics
How to Sort a List by Length of String Followed by Alphabetical Order
Intersection of Two Lists Including Duplicates
Converting List of Tuples into a Dictionary
Difference Between Type(Obj) and Obj._Class_
Python - How to Convert JSON File to Dataframe
Modular Multiplicative Inverse Function in Python
Efficient Way to Add Spaces Between Characters in a String
How to Compare Dates in Django Templates
How to Apply Itertools.Product to Elements of a List of Lists
How to Write Data into CSV Format as String (Not File)
Python Ftp Implicit Tls Connection Issue
Why Is the Value of _Name_ Changing After Assignment to Sys.Modules[_Name_]
Having Trouble Making a List of Lists of a Designated Size
Stopping a Thread After a Certain Amount of Time