PandasとNumpyで大きなDataframeのデータを生成する方法

タイトルの通り、Pandas使ったちょっとしたテストで「それなりに」大きなDataframeを作りたい場合の例です。

import pandas as pd
import numpy as np
import datetime

row_num = 10000000

string_values = ['Python', 'Ruby', 'Java', 'JavaScript', 'PHP','Golang']
df = pd.DataFrame({
    'datetime': np.datetime64(datetime.datetime.now()) + np.random.choice(np.arange(0, 1000000000),row_num),
    'string':np.random.choice(string_values,row_num),
    'int': np.random.randint(0,100,row_num),
    'double': np.random.rand(row_num),
    })

10000000行のDataframeできました。

 datetime    string  int double
0   2019-12-10 08:03:10.784798  Ruby    23  0.889922
1   2019-12-10 08:07:45.987049  Ruby    93  0.488314
2   2019-12-10 08:02:45.030187  Java    36  0.052782
3   2019-12-10 08:01:13.652034  Golang  45  0.814225
4   2019-12-10 07:59:24.717555  Python  35  0.193269
... ... ... ... ...
9999995 2019-12-10 08:10:52.879531  Java    86  0.629834
9999996 2019-12-10 07:59:54.021181  JavaScript  81  0.232827
9999997 2019-12-10 08:04:32.693585  Ruby    70  0.102595
9999998 2019-12-10 08:00:40.013789  PHP 43  0.061625
9999999 2019-12-10 08:00:24.714994  Python  98  0.678055
10000000 rows × 4 columns

上記で生成できたデータです。このカラム構成だと、10000000行で300MB程度です。

df.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 10000000 entries, 0 to 9999999
Data columns (total 4 columns):
datetime    datetime64[ns]
string      object
int         int64
double      float64
dtypes: datetime64[ns](1), float64(1), int64(1), object(1)
memory usage: 305.2+ MB

参考

NumPyのrandomを使った配列操作・乱数生成方法まとめ - DeepAge