The index of my df are strings of company names. Eg Wells Fargo
Sometimes there are excess spaces in-between the words I want to convert to only single spaces. I tried the below but got errors.
**TypeError: expected string or bytes-like object**
df.index=re.sub(' +', ' ', df.index.astype('str').str.strip())
**AttributeError: 'Index' object has no attribute 'apply'**
df.index=df.index.astype('str').str.strip().apply(lambda x: re.sub(' +', ' ', x))
Input df
| Revenue |
Wells Fargo | 1 |
Bank of American| 3 |
Desired output
| Revenue |
Wells Fargo | 1 |
Bank of American| 3 |