0
0

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で一日分の行列を生成して時分秒も入れる

Last updated at Posted at 2018-11-22

インターバルを1秒とした1日分の空のテーブルを作り時分秒のカラムに適切な値を入れるプログラムです.
生成後のデータフレーム行数は86400(= 24 x 60 x 60)となります.
普通に一行ずつ処理するとそれだけで1分とかかかっちゃうので処理時間若干早くなるように考慮.
手元のWindowsマシンで3秒くらいになりました.

sample.ipynb
import pandas as pd
df = pd.DataFrame(index=range(86400),columns=['h','m','s'])

for h in range(24):
    df.loc[range(h*3600, h*3600+3600),['h']] = h

for ms in range(60):
    m_list = []
    for m_ in range(ms*60, ms*60+86400, 3600):
        m_list.extend(list(range(m_, m_+60)))
    df.loc[m_list,['m']] = ms
    df.loc[range(ms, 86400, 60),['s']] = ms

print(df)

時分秒のindexを算出してから一気に代入,ってところがポイントです.

実行結果
image.png
[86400 rows x 3 columns]

0
0
1

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
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?