[英]Pandas count category frequency in a row
The questions I see here are all about groupby and count by columns.我在这里看到的问题都是关于 groupby 和 count by columns。 If I have a dataframe that looks like this:如果我有一个如下所示的数据框:
a b c d e
0 Apple 0.3 Pear 0.4 Apple
I want to count the category frequency of these certain columns: [a,c,e] for each row and create a column for the most frequent category.我想计算这些特定列的类别频率:每行 [a,c,e] 并为最常见的类别创建一列。 In this case it would be column f, value of "Apple" since it has 2 occurrences在这种情况下,它将是列 f,“Apple”的值,因为它出现了 2 次
我们可以做mode
df['f']=df.loc[:,list('ace')].mode(axis=1).iloc[:,0]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.