0
4

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で動的に新たなデータフレームを作る

Last updated at Posted at 2020-02-13

pandasでどうしても、for文を回して一行ずつデータを加工しながら、
insertしたいということがある気がします。

(globで別々のテキストファイルから持ってくるときは不可避。他にもあるはず。自分の場合は、考えるのが面倒で、この方法を取る方がある気がします、それは良くないすね、、)

まぁということで、そういうときのやり方。

# まずcolumnだけのdfを用意
df = pd.DataFrame( columns=['A','B'] )

# 行を作る
for i in range(10000):
    tmp_se = pd.Series( [ i, i**2 ], index=df.columns )

    # dfに行をレコードとして追加
    df = df.append( tmp_se, ignore_index=True )

だと思ってたのですが遅い・・。
以下の方が高速

# 空の辞書
series_dict = {}

# 行を作る
for i in range(10000):
    tmp_se = pd.Series( [ i, i**2 ], index=['A','B'] )
    series_dict[i] = tmp_se #辞書に追加
   
df = pd.DataFrame.from_dict(series_dict, orient="index")


0
4
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
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?