python - 当 groupie 后跟 value_counts() 时如何计算 pandas.Series 范围
问题描述
我有这样的数据:
year = ['2010', '2011-2014', '2013', '2012-2016', '2018-present', '2019', '2015-present', '2015']
products = ['A', 'B', 'C', 'D', 'B', 'E', 'F', 'A']
rating = [4, 2, 2, 3, 1, 1, 2, 2]
data = pd.DataFrame({'Products': products, 'Year': year, 'Rating': rating})
在我的分析中,我想将年份范围转换为单年值(例如,['2010', '2011', '2013', '2014', '2015', '2016', '2017', '2018', '2019', '2020']
),并为其他列添加年份范围的计数。例如对于上面的例子,我想要: {'2010': 'A', '2011': 'B', '2013': 'B', '2014': 'B', '2013': 'c ','2012':'D','2013':'D','2014':'D','2015':'D','2016':'D',...}
我相信我需要它与pandas.cut
分箱相反,但我不知道如何在熊猫中做到这一点
解决方案
使用explode
:
# Extract the range information from the Year column
y = data['Year'].str.extract('(?P<From>\d+)-?(?P<To>\d+|present)?')
y['To'] = y['To'].combine_first(y['From']).replace({'present': '2020'})
y = y.astype('int')
y['Range'] = y.apply(lambda row: range(row['From'], row['To']+1), axis=1)
# The explosion
data['Range'] = y['Range']
data = data.explode('Range')
结果:
Products Year Rating Range
A 2010 4 2010
B 2011-2014 2 2011
B 2011-2014 2 2012
B 2011-2014 2 2013
B 2011-2014 2 2014
C 2013 2 2013
D 2012-2016 3 2012
D 2012-2016 3 2013
D 2012-2016 3 2014
D 2012-2016 3 2015
D 2012-2016 3 2016
B 2018-present 1 2018
B 2018-present 1 2019
B 2018-present 1 2020
E 2019 1 2019
F 2015-present 2 2015
F 2015-present 2 2016
F 2015-present 2 2017
F 2015-present 2 2018
F 2015-present 2 2019
F 2015-present 2 2020
A 2015 2 2015
根据需要重命名列
推荐阅读
- c++ - 如何避免模板函数返回类型的重复?
- javascript - 尝试从nodejs将日期保存到Mysql时日期值不正确
- php - 如何在PHP中不使用循环获取对象数组的索引
- swiftui - 在 SwiftUI 中使用 ForEach 进行迭代时,如何确保视图显示在其他视图之上?
- linux - Jenkins 用户会根据工作名称进行更改,为什么?
- python - 是否有更简单的方法可以在布尔索引时从 Series 对象中检索索引名称?
- javascript - 使用 JavaScript 从当前 HTML、PHP 表单中获取数据
- es6-proxy - 为什么 get 处理程序中的 console.log 接收器值会导致错误?
- django - Django 获取对象的实例并将其传递给表单
- javascript - 如何在 componentDidUpdate 中跟踪 mobx 全局存储的变化