python groupby 然后一些列先保留其他列最后

标签 python pandas

我有一个看起来像的数据框:

cityid    personid    yearstart monthstart yearend monthend  
1          1           2000       01        2001    02
1          1           2001       02        2001    10
1          2           2001       10        2002    10
2          3           2000       01        2002    12
2          4           2005       08        2006    12
person 1city 1连续有两个术语,我想合并这两行并得到:
cityid    personid    yearstart monthstart yearend monthend  
1          1           2000       01        2001    10
1          2           2001       10        2002    10
2          3           2000       01        2002    12
2          4           2005       08        2006    12
所以每一行都有一个唯一的键 {cityid, personid}。
我试过df = df.groupby['cityid','personid'].['yearstart','momthstart'].first()['yearend, monthend'].last()但收到错误消息。
我能问一下如何解决这个问题吗?谢谢!

最佳答案

您可以使用 agg :

(df.groupby(['cityid','persionid'])
   .agg({'yearstart':'first',
         'monthstart':'first',
         'yearend':'last',
         'monthend':'last'})
)
  

关于python groupby 然后一些列先保留其他列最后,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63728086/

相关文章:

python - gensim Doc2Vec vs tensorflow Doc2Vec

python - 每次添加新的 Google Sheet 行时都会触发 AWS Lambda

python - 如何在 python 中 pickle 一个动态创建的嵌套类?

python - Pycharm 3.4.1 - "AppRegistryNotReady: Models aren' t 已加载。Django Rest 框架

python - 从 pandas 中的逗号分隔字符串列中删除重复条目

python - Python 是如何特殊处理 Pandas 中的相等运算符的?

python - 如何对pandas数据框中的复杂条件求和

python - 属性错误 : 'module' object has no attribute 'python_implementation' running pip

python - pandas DataFrame 可以保存非标量值吗?

python - 使用python实现异构csv的数据结构