我很难在Python中组合几个概念;groupby、map和transform。我有一个dataframe,我希望通过基于组转换现有列来创建一个新列。像这样:
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'
我以为我设置得很好,但却很难让它正常工作。谢谢你的指导
修正你的代码
相关问题 更多 >
编程相关推荐