假设我的数据帧如下所示:
column_name
1 book
2 fish
3 icecream|book
4 fish
5 campfire|book
现在,如果我使用df['column_name'].value_counts()
,它会告诉我fish
是最常用的值。在
但是,我希望返回book
,因为第1、3和5行包含单词“book”。在
我知道.value_counts()
将{}识别为一个值,但是有没有一种方法可以通过计算每个列单元格包含某个值的次数来确定最频繁的值,这样“book”将是最频繁的值吗?在
Tags:
使用
collections.Counter
+itertools.chain
:将^{} 与^{} 一起用于
Series
:或者
^{pr2}$Counter
使用列表理解和扁平化:pd.value_counts
也可以将列表传递给
value_counts
函数。注Ijoin
除以|
,然后再除以|
。在get_dummies
这是因为数据是用
^{pr2}$|
作为分隔符的。如果有不同的分隔符,请将其传递给get_dummies
调用df.column_name.str.get_dummies(sep='|').sum()
如果你想把结果排序
pd.factorize
和{请注意,我
join
整个列并再次拆分。在要排序,我们可以像上面那样使用
sort_values
。或者这个相关问题 更多 >
编程相关推荐