Skip to content Skip to sidebar Skip to footer

Python Pandas: Updating Row Based On Value From Another Column

I have a pandas dataframe, df, like: name | grade | grade_type --------------------------- sarah | B | letter alice | A | letter eliza | C | letter beth | 76

Solution 1:

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

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

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

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

defget_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)

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

defget_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)

Post a Comment for "Python Pandas: Updating Row Based On Value From Another Column"