trainのAgeを新しい変数を作り、数値を代入したい。下記の3つのグラフから20~30代の年齢の人がPclass=3の人の生存率が高く、特徴量?を作りたいと思った。train['new_age'].unique()をすると0,1,2,3とでなかった。
1下記のコードが上手くいってない理由
2下記のコードでif文で書くならばどうなりますか?(を作り、数値を代入したい。下記の3つのグラフから20~30代の年齢の人がPclass=3の人の生存率が高く、特徴量?を作りたいと思った。train['new_age'].unique()をすると0,1,2,3とでなかった。
1下記のコードが上手くいってない理由
2下記のコードでif文で書くならばどうなりますか?(ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().)になりできなかった。
3特徴量を作るときにアドバイスがあったらお願いします。(任意)
コード import pandas as pd import matplotlib.pyplot as plt import seaborn as sns train = pd.read_csv('train.csv') fig=plt.figure(figsize=(18,6)) sns.histplot(x=train['Age'], shrink=0.8, bins=12, hue=train['Survived'], multiple="dodge", palette='dark') plt.show() fig=plt.figure(figsize=(10,6)) sns.countplot(x='Survived',hue=train['Pclass'],data=train) fig=plt.figure(figsize=(18,6)) sns.histplot(x=train['Age'], shrink=0.8, bins=12, hue=train['Pclass'], multiple="dodge", palette='dark') plt.show() train['new_age'] = 0 train.loc[(train['Age']<20),'new_age'] = 1 train.loc[(train['Age']<40),'new_age'] = 2 train.loc[(train['Age']<50),'new_age'] = 3 train['new_age'].unique()
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2022/05/09 12:47
2022/05/09 12:52
2022/05/09 12:53