2016-10-13 3 views

답변

3

난 당신이 replace 필요하다고 생각 :

titanic_df['Embarked'] = titanic_df['Embarked'].replace(" ", "S") 

샘플 : 하나 이상의 공백을 대체해야하는 경우

import pandas as pd 

titanic_df = pd.DataFrame({'Embarked':['a','d',' ']}) 
print (titanic_df) 
    Embarked 
0  a 
1  d 
2  

titanic_df['Embarked'] = titanic_df['Embarked'].replace(" ", "S") 

print (titanic_df) 
    Embarked 
0  a 
1  d 
2  S 

또한 정규식과 str.replace를 사용할 수 있습니다.
^ 공백 (들)의 시작을 의미 $는 공백 (들)의 끝을 의미

titanic_df = pd.DataFrame({'Embarked':['a ',' d',' ', ' ']}) 
print (titanic_df) 
    Embarked 
0  a 
1  d 
2   
3 

titanic_df['Embarked'] = titanic_df['Embarked'].str.replace("^\s+$", "S") 
#same output 
#titanic_df['Embarked'] = titanic_df['Embarked'].replace("^\s+$", "S", regex=True) 
print (titanic_df) 
    Embarked 
0  a 
1  d 
2  S 
3  S 
1

또는 당신이 사용할 수를 apply

titanic_df['Embarked'] = titanic_df['Embarked'].apply(lambda x: "S" if x == " " else x) 
관련 문제