Попробуйте,
n= float(len(df.iloc[:, 2:].columns.values))
df['ratio']=df.iloc[:, 2:].apply(lambda x: collections.Counter(x.values).most_common(1)[0][1]/n,axis=1)
Вывод:
A B C D E F G H I J I.1 K ratio
0 32145 Basket 2 2 2 2 2 2 2 2 2 2 1.0
1 43290 Red ball 1 1 0 0 1 1 1 1 1 1 0.8
2 32891 wht ball 4 4 4 0 4 0 4 0 4 4 0.7
3 45328 grn ball 1 1 1 1 1 1 2 1 1 1 0.9
4 34531 blk ball 6 6 6 6 0 0 0 0 6 0 0.5
Метрики производительности:
df=(pd.concat([df]*10000,ignore_index=True))
Мое предлагаемое решение:
start = time.time()
n= float(len(df.iloc[:, 2:].columns.values))
df['ratio']=df.iloc[:, 2:].apply(lambda x: collections.Counter(x.values).most_common(1)[0][1]/n,axis=1)
end = time.time()
print(end - start)
O/P: 0.7386555671691895
* 1012Решение * @ jpp:
start = time.time()
arr = df.iloc[:, 2:]
modes = stats.mode(arr.mask(arr.eq(0)), 1)[0].ravel()
df['ratio'] = arr.eq(modes, axis=0).mean(1)
end = time.time()
print(end - start)
O/P: 1.281557559967041
@ Решение Сандипа Кадапы:
start = time.time()
d = (df.iloc[:, 2:].apply(pd.value_counts, 1).drop(0, 1).max(1)/df.iloc[:, 2:].shape[1])
df['L'] = np.where(d>0.5, df.iloc[:, 2:].max(1), 0)
end = time.time()
print(end - start)
O/P: 73.34089946746826