python - Python将map与groupby和transform结合起来
问题描述
我在 Python 中组合几个概念时遇到了麻烦;groupby、映射和变换。我有一个数据框,我希望通过基于组转换现有列来创建新列。像这样:
s = df[df['type'].eq('office')].groupby(['user','date']).transform('any')
df.loc[:,'type2'] = df['type'].s.map({True:'office',False:'remote'})
所以我的数据框看起来像这样
user date type type2
ron 12/1/19 office office
ron 12/1/19 remote office
april 12/1/19 office office
leslie 12/1/19 remote office
leslie 12/1/19 office office
leslie 2/1/20 office office
但我收到以下错误:
AttributeError: 'Series' object has no attribute 's'
我以为我正确设置了它,但无法让它工作。感谢指导 谢谢
解决方案
修复你的代码
s = df['type'].eq('office').groupby([df['user'],df['date']]).transform('any')
df['type2'] = s.map({True:'office',False:'remote'})
推荐阅读
- azure - 应用 udr 时,Azure 应用程序网关门户概述页面上没有统计数据
- assembly - 汇编语言大加法、乘法和除法
- linux - linux - 在rsync之后,当diff没有时du显示大小差异
- java - 将 EclipseLink 迁移到 Hibernate SQLGrammarException
- javascript - 在 elementor 小部件中显示 wordpress 短代码?
- javascript - Intl.NumberFormat 以挪威克朗格式化价格
- reactjs - state 的值始终是默认值。反应 js 16.12.0
- android - Geolocation.getCurrentPosition 返回错误坐标 (0;0)
- c# - 如何将 IFileFormCollection 中的多个上传文件链接到相应的复杂模型字段?
- python - 用 Python 实现形态桥接