0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

PandasのDataFrameでグループごとにサンプルをN個抜き出す

Last updated at Posted at 2020-10-29

グループ別にN個抜き出したく、その対処方法の一つに。
例えば、列の日別と売上上位n番目までを抜き出したい時とか。

sort_values
並べ替え
groupby
グループ化
.head(n)
グループの中のはじめからn個
.tail()
グループの中のうしろからn個

サンプルコード: 売上上位3番目まで
newdf = df.sort_values(by=['日付','売上'], ascending=[True, False]).groupby('日付').head(3)

参考記事:
https://blog.shikoan.com/pandas-groupby-head/

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?