简体   繁体   中英

Attempting to replace column values with a NaN by loc method

Pandas help, please. I'm trying to replace the values in the "reading_score" column for Thomas High School students that are in the 9th grade. This syntax appears correct but it doesn't appear to be replacing any of the data.

Edit: I must've been tired because I see my syntax error. I'll leave this here, followed by the correction, in case others can use this as a teaching or learning tool. Thanks for stopping by!

student_data_df.loc[(["school_name"] == "Thomas High School") & (student_data_df["grade"] == "9th"), "reading_score"] = np.nan
student_data_df
student_data_df.loc[(student_data_df["school_name"] == "Thomas High School") & (student_data_df["grade"] == "9th"), "reading_score"] = np.nan
student_data_df

Could you do something like this?

(you are just missing student_data_df in student_data_df["school_name"])

def set_reading_score_to_nan(df):
    df.loc[
        (df["school_name"] == "Thomas High School") & (df["grade"] == "9th"),
        "reading_score",
    ] = np.nan
    return df

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM