Change values in pandas dataframe according to value_counts()

后端 未结 2 2002
执念已碎
执念已碎 2020-12-21 02:02

I have following pandas dataframe :

import pandas as pd 
from pandas import Series, DataFrame

data = DataFrame({\'Qu1\': [\'apple\', \'potato\', \'cheese\',         


        
2条回答
  •  情深已故
    2020-12-21 03:01

    I would create a dataframe of same shape where the corresponding entry is the value count:

    data.apply(lambda x: x.map(x.value_counts()))
    Out[229]: 
       Qu1  Qu2  Qu3
    0    1    2    1
    1    2    4    3
    2    3    3    1
    3    2    3    3
    4    3    3    3
    5    2    2    3
    6    3    4    3
    7    2    4    3
    8    1    4    1
    

    And, use the results in df.where to return "other" where the corresponding entry is smaller than 2:

    data.where(data.apply(lambda x: x.map(x.value_counts()))>=2, "other")
    
          Qu1      Qu2     Qu3
    0   other  sausage   other
    1  potato   banana  potato
    2  cheese    apple   other
    3  banana    apple  cheese
    4  cheese    apple  cheese
    5  banana  sausage  potato
    6  cheese   banana  cheese
    7  potato   banana  potato
    8   other   banana   other
    

提交回复
热议问题