前提・実現したいこと
Embarked=Cの時、SurvivedとSexにおいてのFareの平均値を表したグラフを描写したいです。
発生している問題・エラーメッセージ
TypeError: unsupported operand type(s) for -: 'str' and 'float'
該当のソースコード
python
1pivot = train_df[train_df["Embarked"]=='C'][["Survived", "Fare", "Sex"]].groupby(["Survived", "Sex"], as_index=False).mean().sort_values(by="Fare", ascending=False) 2 3pivot_category_0 = pivot[pivot["Survived"]==0] 4pivot_category_1 = pivot[pivot["Survived"]==1] 5 6fig, (axL, axR) = plt.subplots(ncols=2, figsize=(10,4)) 7 8axL.bar(pivot_category_0["Sex"], pivot_category_0["Fare"]) 9axL.set_title('Embarked=C | Survived=0') 10axL.set_xlabel('Sex') 11axL.set_ylabel('Fare') 12 13axR.bar(pivot_category_1["Sex"], pivot_category_1["Fare"]) 14axR.set_title('Embarked=C | Survived=1') 15axR.set_xlabel('Sex') 16axR.set_ylabel('Fare') 17 18plt.show()
試したこと
pivot_category_0とpivot_category_1です。
Survived Sex Fare
1 0 male 13.911732
0 0 female 10.904633
Survived Sex Fare
2 1 female 13.211733
3 1 male 12.916667
Sexが数値じゃないからいけないのでしょうか?
検索をかけてもstrからintに変換するものしかなかったです。
どこを修正すればいいですか??
あなたの回答
tips
プレビュー