Is there a way to trim/strip whitespace in multiple columns of a pandas dataframe?
Question:
I have a pandas dataframe with 5 columns and 3 of those columns are string columns. I want to trim all the leading and trailing whitespaces in those 3 columns. Is there a way to achieve this in one go.
ID col_1 col_2 col_3 col_4
0 1 AA XX 200 PP
1 2 BB YY 300 QQ
2 3 CC ZZ 500 RR
I want to trim 'col_1', 'col_2', 'col_4'
I know df['col_1'].str.strip()
works on a individual column. But can i do all the columns at one go?
Answers:
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())
df.columns = df.columns.str.strip()
I have a pandas dataframe with 5 columns and 3 of those columns are string columns. I want to trim all the leading and trailing whitespaces in those 3 columns. Is there a way to achieve this in one go.
ID col_1 col_2 col_3 col_4
0 1 AA XX 200 PP
1 2 BB YY 300 QQ
2 3 CC ZZ 500 RR
I want to trim 'col_1', 'col_2', 'col_4'
I know df['col_1'].str.strip()
works on a individual column. But can i do all the columns at one go?
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())
df.columns = df.columns.str.strip()