我有这样一个数据帧:
import pandas as pd
df = pd.DataFrame({'A':['a','b','c','d'],'B':['a','b','c','x'],'C':['y','b','c','d']})
df
A B C
0 a a y
1 b b b
2 c c c
3 d x d
我想找出每一行中最常见的字符,以及与共识的差异总数:
A B C Consensus
0 a a y a
1 b b b b
2 c c c c
3 d x d d
Total 0 1 1 0
运行循环是一种方法,但它似乎效率低下:
consensus = []
for idx in df.index:
consensus.append(df.loc[idx].value_counts().index[0])
df['Consensus'] = consensus
(以此类推)
有没有一种直接的方法来取得共识并计算分歧?你知道吗
您可以使用
mode
来获得一致性值:注意文档中的注意事项:
要计算每一列的一致性差异,您可以与
ne
进行比较,然后求和:相关问题 更多 >
编程相关推荐