DataFrame中统计某几列中字符出现次数并比较

aa = pd.DataFrame({'id':['xx','yy','zz','mm'], 'a':['10','10','11',np.nan],'b':['11','12','11',np.nan],'c':['10','12','',np.nan],'d':['10','10','10',np.nan],'e':['10','10','11','']})
aa

 DataFrame中统计某几列中字符出现次数并比较 

def map_func1(x):
    if (x['a'] == x['b'] == x['c'] == x['d'] == ''):
        return 'www'
    else:
        li = list(x[['a','b','c','d']])
        print(li)
        set1 = set(li)
        dict1 = {}
        for item in set1:
            dict1.update({item:li.count(item)})
        print(dict1)
        return max(dict1,key=dict1.get)

  

aa['xinlai'] = aa.apply(map_func1,axis=1)  #axis控制按行还是按列

  DataFrame中统计某几列中字符出现次数并比较

上一篇:数据类型


下一篇:02 在idea中熟悉python的基本语法