Make Upper Case And Replace Space In Column Dataframe
for a specific column of a pandas dataframe I would like to make the elements all uppercase and replace the spaces import pandas as pd df = pd.DataFrame(data=[['AA 123',00],[99,10
Solution 1:
str.upper
with replace
df['A'] = df.A.str.upper().replace('\s+', '', regex=True).fillna(df['A'])
A B
0 AA123 0
1 99 10
2 BB12 10
Solution 2:
You can simplify this a lot by using numpy.where
to select the rows you want to modify:
import pandas as pd
import numpy as np
df = pd.DataFrame(data=[['AA 123',00],[99,10],['bb 12',10]],columns=['A','B'],index=[0,1,2])
df['A'] = np.where(df['A'].apply(lambda x: isinstance(x, str)),
df['A'].str.upper().str.replace(r'\s', ''),
df['A'])
Post a Comment for "Make Upper Case And Replace Space In Column Dataframe"