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

后端 未结 2 1089
暗喜
暗喜 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:44

    Use isin to create a boolean mask and use loc to set the rows that meet the condition to the desired new value:

    In [37]:
    temp.loc[temp['c'].isin(mylist),['a','b']] = 0
    temp
    
    Out[37]:
       a  b  c
    0  0  0  p
    1  2  3  q
    2  3  4  r
    3  4  5  s
    4  0  0  t
    5  6  7  u
    

    result of the inner isin:

    In [38]:
    temp['c'].isin(mylist)
    
    Out[38]:
    0     True
    1    False
    2    False
    3    False
    4     True
    5    False
    Name: c, dtype: bool
    
    0 讨论(0)
  • 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
    
    0 讨论(0)
提交回复
热议问题