I have a large excel file and I am looking at a single row in it and I want to remove leading and trailing whitespaces from text entries.
My code is as follows:
df = pd.read_excel(excel_file_name)
layer = df.loc[[layer_index]] # layer is a single row in df and layer_index is an integer
print(layer.iloc[:,37:42])
for col in layer.columns:
if type(layer[col]) == str:
layer[col] = layer[col].map(str.strip)
print(layer.iloc[:,37:42])
Column 41 contains a lot of leading and trailing whitespace so that the excel entry there is something like Cell 3 ramp . The output is:
Unnamed: 37 Unnamed: 38 Unnamed: 39 Unnamed: 40 Unnamed: 41
22 NaN Ga2 NaN Cell 3 ramp NaN
Unnamed: 37 Unnamed: 38 Unnamed: 39 Unnamed: 40 Unnamed: 41
22 NaN Ga2 NaN Cell 3 ramp NaN
But I want it to be such that the whitespaces are removed:
Unnamed: 37 Unnamed: 38 Unnamed: 39 Unnamed: 40 Unnamed: 41
22 NaN Ga2 NaN Cell 3 ramp NaN
Unnamed: 37 Unnamed: 38 Unnamed: 39 Unnamed: 40 Unnamed: 41
22 NaN Ga2 NaN Cell 3 ramp NaN
Why doesn't my code work?
Example
we need minimal and reproducible example.
df
Code
this is code of removing white space from string columns of all.
df