remove leading and lagging spaces dataframe python

cols = df.select_dtypes(['object']).columns
df[cols] = df[cols].apply(lambda x: x.str.strip())
print (df)
                     A    B     C
0                  A b  NaN   3.0
1                  NaN  NaN   3.0
2               random  NaN   4.0
3  any txt is possible  2 1  22.0
4                       NaN  99.0
5                 help  NaN   NaN

3.83
6
Telaclavo 105 points

                                    df = df.applymap(lambda x: x.strip() if isinstance(x, str) else x)
print (df)
                     A    B     C
0                  A b    2   3.0
1                  NaN    2   3.0
2               random   43   4.0
3  any txt is possible  2 1  22.0
4                        23  99.0
5                 help   23   NaN

3.83 (6 Votes)
0
Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
Remove the leading spaces from all entries in the column python code to remove leading spaces from a column in pandas How to remove leading and trailing spaces in pandas dataframe remove leading and trailing spaces in python pandas remove leading spaces in pandas column remove leading spaces in pandas remove leading and trailing character with Pandas dataframe dataframe remove leading space pandas clean inial and leading spaces from columnt names remove leading and trailing whitespace pandas pandas remove leading and trailing whitespace pandas remove leading trailing spaces pandas remove leading spaces from entire dataframe remove leading spaces dataframe pandas remove leading spaces pandas remove leading and trailing spaces pandas dataframe remove leading and trailing spaces remove leading and lagging spaces dataframe python df strip trim strin gin panda sereis pandas str.strip remove spaces in dataframes remove trailing and leading spaces from dataframe pandas striping leading and trailing rows in python pandas strip leading and trailing whitespace pandas remove trailing and leading spaces remove whitespace in column pandas pandas dataframe strip columns pandas strip whitespace from column how to strip all whitespace in dataframe pandas trim pandas pandas strip whitespace entire column pandas remove one whitespace from column pandas remove whitespace from a column pandas trims spaces pandas trim column remove trailing spaces pandas column how to trim in pandas dataframe trim string python pandas how to trim in pandas Function to remove leading and trailing white-spaces in the data frame strip column values pandas trim column in python pandas remove string from row strip leading and trailing whitespace pandas in all values .str.strip() python dataframe
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source