我正在Codacademy的Predict Titanic Survival项目中工作,我正在尝试更新CSV文件的列,将“性别”列的形式从male / female更改为0/1,但出现错误。有人可以帮忙!
Traceback (most recent call last):
File "script.py", line 13, in <module>
passengers['Sex']=passengers(['Sex']).map({'female': 1, 'male': 0})
TypeError: 'DataFrame' object is not callable
代码:
import codecademylib3_seaborn
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
# Load the passenger data
passengers = pd.read_csv('passengers.csv')
print (passengers)
# Update sex column to numerical
passengers['Sex']=passengers(['Sex']).map({'female': 1, 'male': 0})
print (passengers.head(1))
请帮助!
答案 0 :(得分:0)
正如@Uzay Macar在评论中提到的那样,如果您删除了括号,它会很好地工作,但这是完成该任务的好方法。
train["Sex"][train["Sex"] == "male"] = 0
train["Sex"][train["Sex"] == "female"] = 1
或
data.loc[data["Sex"] == "male", "Sex"] = 0
data.loc[data["Sex"] == "female", "Sex"] = 1
答案 1 :(得分:0)
您也可以尝试从sklearn进行编码