Proper way to use iloc in Pandas

前端 未结 4 1171
故里飘歌
故里飘歌 2020-12-14 04:39

I have the following dataframe df:

print(df)

    Food         Taste
0   Apple        NaN
1   Banana       NaN
2   Candy        NaN
3   Milk         NaN
4            


        
4条回答
  •  一整个雨季
    2020-12-14 05:25

    I prefer to use .loc in such cases, and explicitly use the index of the DataFrame if you want to select on position:

    df.loc[df.index[0:2], 'Taste'] = 'good'
    df.loc[df.index[2:6], 'Taste'] = 'bad'
    

提交回复
热议问题