Reputation: 43
I use the default Pandas csv reading to import some data as followed:
df = pd.read_csv('data_file.csv')
The data frame I got is as below:
Force [N] Stress [MPa]
0 0.000000 2.230649e-13
1 0.014117 1.071518e-01
2 0.135255 3.365490e+00
The data frame column I got here has spaces before the actual name (illustrated below) so that I could not use the actual column name string to access the column afterwards.
>>> df.columns
Index([' Force [N]', ' Stress [MPa] '])
I would like to remove the space before the string, but keep the space within the column name string. Tried the following one but it will remove all spaces so the column name got changed as well.
df.columns=df.columns.str.replace(' ','');
Is there anyway to strip the white spaces to the left of column name when importing csv? I would like the column name to be like:
'Force [N]','Stress [MPa]'
Upvotes: 4
Views: 2188
Reputation: 23217
You can use Index.str.strip()
(which is the Index version of Series.str.strip()
), as follows:
df.columns = df.columns.str.strip()
Input:
print(df.columns)
Index([' Force [N]', ' Stress [MPa] '], dtype='object')
Output:
print(df.columns)
Index(['Force [N]', 'Stress [MPa]'], dtype='object')
Upvotes: 1
Reputation: 7045
pandas.Series.str.replace
to replace occurrences of ^[ ]+|[ ]+$
(one or more spaces at the start or end of the column names) with ""
(nothing)
df.columns = df.columns.str.replace("^[ ]+|[ ]+$", "", regex=True)
Upvotes: 2
Reputation: 35646
To avoid post-processing the column data set skipinitialspace=True
to pd.read_csv
:
df = pd.read_csv('data_file.csv', skipinitialspace=True)
df
:
Force [N] Stress [MPa]
0 0.000000 2.230649e-13
1 0.014117 1.071518e-01
2 0.135255 3.365490e+00
df.columns
:
Index(['Force [N]', 'Stress [MPa]'], dtype='object')
data_file.csv
Force [N], Stress [MPa]
0.000000, 2.230649e-13
0.014117, 1.071518e-01
0.135255, 3.365490e+00
Upvotes: 6