Python Pandas : creating a dataframe using a function for one of the fields

标签 python pandas kaggle

我正在尝试创建一个数据框,其中一个字段是使用函数计算的。为此,我使用以下代码:

import pandas as pd

def didSurvive(sex):
    return int(sex == "female")


titanic_df = pd.read_csv("test.csv")
submission = pd.DataFrame({
    "PassengerId": titanic_df["PassengerId"],
    "Survived": didSurvive(titanic_df["Sex"])
})
submission.to_csv('titanic-predictions.csv', index=False)

当我运行此代码时,出现以下错误:

D:\Documents\kaggle\titanic>python predictor.py
File "predictor.py", line 3
def didSurvive() {
^
SyntaxError: invalid syntax
D:\Documents\kaggle\titanic>python predictor.py
D:\Documents\kaggle\titanic>python predictor.py
D:\Documents\kaggle\titanic>python predictor.py
Traceback (most recent call last):
File "predictor.py", line 10, in
"Survived": didSurvive(titanic_df["Sex"])
File "predictor.py", line 4, in didSurvive
return int(sex == "female")
File "C:\Python34\lib\site-packages\pandas\core\series.py", line 92, in wrapper
"{0}".format(str(converter)))
TypeError: cannot convert the series to
D:\Documents\kaggle\titanic>

我认为正在发生的事情是我试图在一系列 bool 值而不是单个 bool 值上运行 int() 。我该如何解决这个问题?

最佳答案

要转换Series的数据类型,您可以使用astype()函数,这应该可以:

def didSurvive(sex):
    return (sex == "female").astype(int)

关于Python Pandas : creating a dataframe using a function for one of the fields,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41081882/

相关文章:

python - DRF APITestCase 不将 `multipart` 与其他参数一起使用

python - 使用python从sql查询中的文本文件调用日期

tensorflow - 如何将自定义数据集生成器与 TPU 结合使用?

python - 将附加参数传递给 Pandas 自定义访问器

amazon-ec2 - 如何在 Kaggle 中的 AWS 机器上工作?

从 Kaggle 读取数据集

python - RobertaForSequenceClassification 的对数和概率代表什么?

python - 为什么我的 Django ModelForm 不会引发 unique_together 约束的验证错误?

python - 使用 Axios 响应 JS

python - 如何使用条形图绘制最小/最大条形图