本文介绍了单个变量的频率表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当天最后一个新手熊猫问题:如何为单个系列生成表格?
One last newbie pandas question for the day: How do I generate a table for a single Series?
例如:
my_series = pandas.Series([1,2,2,3,3,3])
pandas.magical_frequency_function( my_series )
>> {
1 : 1,
2 : 2,
3 : 3
}
很多谷歌搜索使我进入了Series.describe()和pandas.crosstabs,但是这些都不满足我的需要:一个变量,按类别计数.哦,如果它适用于不同的数据类型(字符串,整数等),那就太好了.
Lots of googling has led me to Series.describe() and pandas.crosstabs, but neither of these does quite what I need: one variable, counts by categories. Oh, and it'd be nice if it worked for different data types: strings, ints, etc.
推荐答案
也许是.value_counts()
?
>>> import pandas
>>> my_series = pandas.Series([1,2,2,3,3,3, "fred", 1.8, 1.8])
>>> my_series
0 1
1 2
2 2
3 3
4 3
5 3
6 fred
7 1.8
8 1.8
>>> counts = my_series.value_counts()
>>> counts
3 3
2 2
1.8 2
fred 1
1 1
>>> len(counts)
5
>>> sum(counts)
9
>>> counts["fred"]
1
>>> dict(counts)
{1.8: 2, 2: 2, 3: 3, 1: 1, 'fred': 1}
这篇关于单个变量的频率表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!