python - 根据元组的条件进行计数
问题描述
我想计算以下数据集上的员工人数
for combined in dic.iteritems():
"""prints employees by employer and year"""
print(combined)
('a', {2001: 12, 2001: 13, 2001: 15, 2004: 28, 1999: 12})
('c', {2000: 23, 2003: 15, 2004: 7, 2005: 24})
('b', {2001: 13, 2002: 13, 2012: 12})
('e', {2002: 7, 2004: 30, 2005: 7})
('d', {2001: 7, 2002: 28, 2010: 24})
('g', {2000: 7, 2009: 7, 2010: 333})
('f', {2005: 30, 2006: 7, 1999: 12})
for employer, yearIndividuals in dic.iteritems():
print(employer)
"""iterate over the dictionary to find the combinations"""
for year, individuals in yearIndividuals.iteritems():
#print(employer, individuals, year)
x=employer, individuals, year
for grp, elemts in groupby(x, (lambda x: x[1], x[0])):
print(grp, len(list(elmts)))
我想要以下格式的输出:
employer, year, employee
a, 2001, 3
a, 2004, 1
a, 1999, 1
c, 2000, 1
c, 2003, 1
c, 2004, 1
c, 2005, 1
这就是我需要的:我正在尝试计算人们换工作的概率。x 可能在第 1 年为公司 z 工作,然后在第 2 年切换到公司 a。
我试图找出这种转变是如何发生的方式。
假设该表有三列employer
-employee
和year
。
在我上面的例子中,字母a
表示雇主,而数字12
等表示雇员。
我该怎么做?
一般来说,我的要求是将雇主与个人相匹配并计数
解决方案
字典中不能有相同的键(年份)。但是,您确实可以列出特定年份雇用的员工列表,例如2001: [12, 13, 15]
. 然后列出很简单
for employer in dic:
for year in dic[employer]:
for employee in dic[employer][year]:
print employer, year, employee
估计员工转换工作的概率的简单方法,即在记录期间有两个以上的工作是用有两个以上工作的员工人数除以员工人数
推荐阅读
- android - 如何将软件原始文件夹中的音乐设置为默认闹钟歌曲?
- javascript - onPanResponderTerminate 仅在已发布的 APP 上触发 - React Native
- r - 有没有办法只在 R 的数据框中保留重复的行?
- github - Kaggle/GitHub Notebook 不会运行 RMD 文件:上传错误
- sql - 如何允许学生在此页面中注入 DROP TABLE?
- javascript - 在 ChartJS 中更改边框的样式
- php - 通过 PHP API 的 PayPal 智能按钮补丁订单
- python - 权限错误,没有这样的文件或目录
- flutter - 颤动中的阴影模糊效果
- python - 循环一个列表并获得一个新的列表,在 python 中具有相同值的起始索引