How to use pandas apply function on all columns of some rows of data frame

后端 未结 2 1094
暗喜
暗喜 2021-01-23 04:58

I have a dataframe. I want to replace values of all columns of some rows to a default value. Is there a way to do this via pandas apply function

<
2条回答
  •  无人共我
    2021-01-23 05:52

    NumPy based method would be to use np.in1d to get such a mask and use it like so -

    mask = np.in1d(temp.c,mylist)
    temp.ix[mask,temp.columns!='c'] = 0
    

    This will replace in all columns except 'c'. If you are looking to replace in specific columns, say 'a' and 'b', edit the last line to -

    temp.ix[mask,['a','b']] = 0
    

提交回复
热议问题