【问题标题】:how to add function (def) to dataset for more then one column ? can anyone help me out pls如何将函数(def)添加到数据集中超过一列?谁能帮帮我
【发布时间】:2021-05-18 11:07:49
【问题描述】:
def total_score(x, y):
if x >= 35 and y >= 35:
return 'pass'
return 'fail'
st['total_score'] = st[['pretest','posttest']].apply(total_score)
TypeError: total_score() missing 1 required positional argument: 'y'
【问题讨论】:
标签:
python
function
dataset
【解决方案1】:
您可以逐行应用函数:
def row_total_score(row):
if row['pretest] >= 35 and row[posttest] >= 35:
return 'pass'
return 'fail'
st['total_score'] = st.apply(row_total_score)
你也可以把你的函数写成
def row_total_score(row):
if min(row['pretest], row[posttest]) >= 35:
return 'pass'
return 'fail'
或
def row_total_score(row):
return 'pass' if min(row['pretest], row[posttest]) >= 35 else 'fail'