0

I am using below code:

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

Output:

I am getting the complete row as NaN, Need only "reading_score" as Nan

enter image description here

It_is_Chris
  • 13,504
  • 2
  • 23
  • 41
SHAMAY
  • 1
  • 1
  • 3
    `student_data_df.loc[(student_data_df["school_name"] == "Thomas High School") & (student_data_df["grade"] == "9th"), 'reading_score']=np.nan ` you are currently assign nan to the whole row so you need to add the column name to `loc` - i.e., `df.loc[row_indexer, column_indexer]` – It_is_Chris Jan 26 '22 at 19:35
  • 1
    Please don't post images of code, data, or Tracebacks. Copy and paste it as text then format it as code (select it and type `ctrl-k`). [How to make good reproducible pandas examples](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) – wwii Jan 26 '22 at 19:40
  • ... `row_indexer = (student_data_df["school_name"] == "Thomas High School") & (student_data_df["grade"] == "9th"); col_indexer = "reading_score"; student_data_df.loc[row_indexer,col_indexer]=np.nan`, [https://pandas.pydata.org/docs/user_guide/indexing.html](https://pandas.pydata.org/docs/user_guide/indexing.html) – wwii Jan 26 '22 at 19:43
  • Thanks very much Chris, It works!! – SHAMAY Jan 26 '22 at 20:00

0 Answers0