複数の条件を一行のif分で書き表したいです。
以下のコードだとエラーが発生します。
Python
1# Get title 2titles = [] 3def get_title(x): 4 title = x.split(",")[1].split(".")[0].strip() 5 if not ((title == "Mr") & (title = "Mrs")) & (title == "Miss")) & (title == "Master"): 6 title = "Rare" 7 else: 8 title = title 9 return title 10# Apply the above function 11df["Title"] = df["Name"].map(get_title)
因みに、元のデータはkaggleの、Titanicのデータで上記のコードを書くに至るまで、以下のコードを実行しています。
Python
1# Import the column datasets 2df = pd.read_csv("train.csv").drop(["PassengerId", 3 "Cabin","Ticket"], axis=1) 4 5# Change the dtype 6df["Pclass"] = df["Pclass"].astype(str) 7 8df["Family_size"] = df["SibSp"] + df["Parch"] 9 10# Handle the missing value:確定 11df["Embarked"][df["Embarked"].isnull() == True] = "S" 12 13# Impute the age 14df["Age"] = Imputer(strategy="mean",axis=0).fit_transform(df["Age"].reshape(-1,1))
よろしくお願いいたします。
回答1件
あなたの回答
tips
プレビュー