Using str.split().agg("".join)
Ex:
df['Product'] = df['Product'].str.split().agg("".join)
#or
#df['Product'] = df['Product'].str.replace(r"(\s+)", "")
print(df)
Output:
Product
0 Prod1
1 Prod1
2 Prod2
3 Prod2
4 Prod2
5 Prod3
6 Prod3
Answer from Rakesh on Stack OverflowUsing str.split().agg("".join)
Ex:
df['Product'] = df['Product'].str.split().agg("".join)
#or
#df['Product'] = df['Product'].str.replace(r"(\s+)", "")
print(df)
Output:
Product
0 Prod1
1 Prod1
2 Prod2
3 Prod2
4 Prod2
5 Prod3
6 Prod3
Let's try str.replace with the following pattern to remove spaces between Prod and digits.
df['Product'] = df.Product.str.replace('(Prod)(\s+)(\d)', r'\1\3')
Output:
Product
0 Prod1
1 Prod1
2 Prod2
3 Prod2
4 Prod2
5 Prod3
6 Prod3 and so on
How to remove multiple white spaces from data frame
python - Remove Multiple Blanks In DataFrame - Stack Overflow
python - Removing unnecessary spaces in a string column - Stack Overflow
python - How to remove excess spaces in-between words in dataframe index? - Stack Overflow
I`m storing a text file as a dataframe, and I cant remove the stubborn white space:
document = docx2python("R/survey.docx")
lines = document.text.split('\n')
self.content = pd.DataFrame(lines, columns=['text'])
self.content = self.content['text'].str.strip()
self.content = self.content.str.replace(r'\s{2,}','',regex=True)Oddly enough it removes some of the whitespace, but not all of it such as the tabs
It's probably easier to process the dictionary before constructing the dataframe. You also need to account for leading space in any of the strings.
import pandas as pd
import re
foo={'testing':['this is test A',' this is test B',' this is test C ',' this is test D',' this is test E ']}
foo['testing'] = [re.sub('\s+', ' ', s.strip()) for s in foo['testing']]
foo = pd.DataFrame(foo, columns=['testing'])
print(foo)
Output:
testing
0 this is test A
1 this is test B
2 this is test C
3 this is test D
4 this is test E
Without doing a complicated lookarounds, you can do the job in two steps. First remove leading and trailing space; then use regex to replace space inside the strings.
foo['testing'] = foo['testing'].str.strip().str.replace(r'\s+', ' ', regex=True)
print(foo)
testing
0 this is test A
1 this is test B
2 this is test C
3 this is test D
4 this is test E
df.index = df.index.str.replace(r'\s+', ' ', regex=True).str.strip()
In your first attempt, you are trying to pass a Pandas Index of strings to re.sub, which takes a string.
apply would work if the company names were stored as a data frame column. However as the error message says, apply is not implemented for the index.
Use str.split() on string and then df.rename on index. See each step below.
import pandas as pd
# making your df
d = {'index':['Wells Fargo'], 'col1':[123], 'col2':[123]}
df = pd.DataFrame(d)
df = df.set_index('index')
# get list of index strings
index_str_list = [strings for strings in df.index]
# format spaces and append to new list
new_list = []
for i in index_str_list:
s1,s2 = i.split()
s = "{:6}{:}".format(s1,s2) # set your distance
new_list.append(s)
# change index value
for old,new in zip(index_str_list, new_list):
df.rename(index={old:new}, inplace=True)
print(df)
Output:
col1 col2
index
Wells Fargo 123 123
You could use apply:
df = df.applymap(lambda x: " ".join(x.split()) if isinstance(x, str) else x)
An idea would be to do a combination of:
regexto remove duplicate spaces (e.g " James Bond" to " James Bond")str.stripto remove leading/trailing spaces (e.g " James Bond" to "James Bond").
You could do this one of two ways:
1. On the whole DataFrame:
df = df.replace("\s+", " ", regex=True).apply(lambda x: x.str.strip())
2. On each column individually:
for col in ["Name", "Country"]:
df[col] = df[col].replace("\s+", " ", regex=True).str.strip()