pandas DataFrame set value on boolean mask

前端 未结 4 1236
隐瞒了意图╮
隐瞒了意图╮ 2020-12-18 20:07

I\'m trying to set a number of different in a pandas DataFrame all to the same value. I thought I understood boolean indexing for pandas, but I haven\'t found any resources

4条回答
  •  暖寄归人
    2020-12-18 20:43

    I'm not 100% sure but I suspect the error message relates to the fact that there is not identical treatment of missing data across different dtypes. Only float has NaN, but integers can be automatically converted to floats so it's not a problem there. But it appears mixing number dtypes and object dtypes does not work so easily...

    Regardless of that, you could get around it pretty easily with np.where:

    df[:] = np.where( mask, 30, df ) 
    
        A   B
    0  30  30
    1   2   b
    2  30   f
    

提交回复
热议问题