为字典的值获取 value_counts()
Get value_counts() for values of a dictionary
我非常熟悉如何从 pd.Series
return value_counts
。但是,我如何从字典的值中获取值计数?
假设我在字典中有以下元组 L
:
L = {1: (13600, 14797),
2: (14700, 14700),
3: (14700, 10400),
4: (14600, 17200),
5: (13600, 14797),
6: (14600, 17200),
7: (14700, 10400),
8: (14700, 10400),
9: (12800, 14770)}
如何从 L
中得到 value_counts
看起来像:
(14700, 10400) 3
(13600, 14797) 2
(14600, 17200) 2
(14700, 14700) 1
(12800, 14770) 1
这是我目前所拥有的。但是,我认为字典键 1-9 妨碍了我,因为我收到错误 list object is not callable.
list = [(k, v) for k, v in L.items()]
S = set(L)
F = {}
for i in list(S):
F[i] = list.count(i)
也许使用 from collections import Counter
是个好主意?
from collections import Counter
dict(Counter([j for i,j in L.items()]))
使用标准库中的collections.Counter
:
Counter(L.values())
你可以试试这个:
L = {1: (13600, 14797),
2: (14700, 14700),
3: (14700, 10400),
4: (14600, 17200),
5: (13600, 14797),
6: (14600, 17200),
7: (14700, 10400),
8: (14700, 10400),
9: (12800, 14770)}
vals = [ v for v in L.values()]
counts = []
for i in vals:
counts.append((i, vals.count(i)))
set(counts)
你提到了pandas.Series.value_counts()
,你为什么不试试
pd.Series(L).value_counts()
给出:
(14700, 10400) 3
(14600, 17200) 2
(13600, 14797) 2
(12800, 14770) 1
(14700, 14700) 1
dtype: int64
我非常熟悉如何从 pd.Series
return value_counts
。但是,我如何从字典的值中获取值计数?
假设我在字典中有以下元组 L
:
L = {1: (13600, 14797),
2: (14700, 14700),
3: (14700, 10400),
4: (14600, 17200),
5: (13600, 14797),
6: (14600, 17200),
7: (14700, 10400),
8: (14700, 10400),
9: (12800, 14770)}
如何从 L
中得到 value_counts
看起来像:
(14700, 10400) 3
(13600, 14797) 2
(14600, 17200) 2
(14700, 14700) 1
(12800, 14770) 1
这是我目前所拥有的。但是,我认为字典键 1-9 妨碍了我,因为我收到错误 list object is not callable.
list = [(k, v) for k, v in L.items()]
S = set(L)
F = {}
for i in list(S):
F[i] = list.count(i)
也许使用 from collections import Counter
是个好主意?
from collections import Counter
dict(Counter([j for i,j in L.items()]))
使用标准库中的collections.Counter
:
Counter(L.values())
你可以试试这个:
L = {1: (13600, 14797),
2: (14700, 14700),
3: (14700, 10400),
4: (14600, 17200),
5: (13600, 14797),
6: (14600, 17200),
7: (14700, 10400),
8: (14700, 10400),
9: (12800, 14770)}
vals = [ v for v in L.values()]
counts = []
for i in vals:
counts.append((i, vals.count(i)))
set(counts)
你提到了pandas.Series.value_counts()
,你为什么不试试
pd.Series(L).value_counts()
给出:
(14700, 10400) 3
(14600, 17200) 2
(13600, 14797) 2
(12800, 14770) 1
(14700, 14700) 1
dtype: int64