且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Python pandas:根据另一列的值更新行

更新时间:2022-12-12 09:36:39

DataFrame不更新的原因是因为

The reason that your DataFrame doesn't update is because rows returned from iterrows(): are copies. And you're working on that copy.

您可以使用从返回的index迭代并直接操作DataFrame:

You can use the index returned from iterrows and manipulate DataFrame directly:

for index, row in df.iterrows():
    grade_val = int(row.grade.values[0])
    if grade_val > 80:
        df.loc[index, 'grade'] = 'A'
    ...

或者如您所说,您可以使用 df. apply(),并向其传递一个自定义函数:

Or as you said you can use df.apply(), and pass it a custom function:

def get_grades(x):
    if x['grade_type'] == 'letter':
        return(x['grade_val']) 
    if x['grade_val'] > 80:
        return "A"
    ...


df['grade'] = df.apply(lambda x: get_grades(x), axis=1)

您还可以在lambda中使用if else来检查x['grade_type']是否为数字,如下所示,使用看起来更容易阅读的数字.

You can also use if else in your lambda to check if x['grade_type'] is numeric as follows, use the one that looks easier to read.

def get_grades(grade_val):
    if grade_val > 80:
        return "A"
    ...

df['grade'] = df.apply(lambda x: get_grades(x['grade']) 
                       if x['grade_type'] == 'numeral' else x['grade'], axis=1)