2
3

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 1 year has passed since last update.

pythonのpandasで、データフレームをN件ずつに分割して処理する

Posted at

結論

pandasのデータフレームをdfという変数に入れていたとします。N件ずつに分割したい場合は[df[i:i+N] for i in range(0, len(df), N)]で分割できます。

サンプルコード

dfには既に205件のデータ(「商品番号」「単価」)が入ったデータフレームであるとします。

.py
import pandas as pd

# ...

# データフレームをN件ずつに分割
N = 10 
splited_df = [df[i:i+N] for i in range(0, len(df), N)]

for part in splited_df:
    print(part)

結果

       商品番号     単価
0  9QYSNZB1  16000
1  IK7H29YP  15000
2  W5X9B5YL  27000
3  2GU990CA  15000
4  U67GM6E5  17000
5  1ORHZ5ZA  29000
6  C8EIXRZZ  10000
7  GG62Y92U  30000
8  7TNHTAVP  18000
9  2SHES5QY   8000
        商品番号     単価
10  K7P414PJ   5000
11  JNGQ8LZC  30000
12  WUPUZSB5  20000
13  HQYZQVD7   8000
14  JIKNL5RT  28000
15  FSWG9LOI   2000
16  AUQJJ9BC  17000
17  DYKUV6N6   5000
18  0KABUAOV   5000
19  6VDDMRPY   4000
...
         商品番号     単価
190  4HM62T2P   7000
191  TRNHYDSP  25000
192  F2G6SPY0  13000
193  A9RV72U0  27000
194  QXSS6GHA  16000
195  FKR56OE2  22000
196  IHEV3TU2  28000
197  D9KWQLM4   1000
198  MHORUEJP   2000
199  N423U8BC  29000
         商品番号     単価
200  F4ABFN26  15000
201  QXN3L9ML   4000
202  5XEC4SHW   7000
203  W1JOXZS4  29000
204  D6NWMVFI  22000

以上です。

2
3
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
2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?