vishakha abnave
vishakha abnave

Reputation: 17

remove whitespace from end of dataframe

I want to remove whitespace after all data in the excel table using panda in Jupiter notebook. foreg:

| A header               | Another header    |
| --------               | --------------    |
| First**whitespace**    | row               |
| Second                 | row**whitespace** |

output:

| A header | Another header |
| -------- | -------------- |
| First    | row            |
| Second   | row            |

Upvotes: 1

Views: 32

Answers (1)

jezrael
jezrael

Reputation: 862681

If all columns are strings use rstrip in DataFrame.applymap:

df = df.applymap(lambda x: x.rstrip())

Or Series.str.rstrip for columns in DataFrame.apply:

df = df.apply(lambda x: x.str.rstrip())

If possible some non strings (non object) columns is possible filter columns names:

cols = df.select_dtypes(object).columns
df[cols] = df[cols].apply(lambda x: x.str.rstrip())

Upvotes: 1

Related Questions