次のようなデータフレームがあります:
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
都市 1 の人 1 には 2 つの用語が連続しているため、これら 2 つの行を結合して次を取得したいと思います。
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()
しかし、エラーメッセージが表示されます。
これを修正する方法を聞いてもいいですか?ありがとう!
------------------------
集約を使用できます。
(df.groupby(['cityid','persionid'])
.agg({'yearstart':'first',
'monthstart':'first',
'yearend':'last',
'monthend':'last'})
)
0