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

pandas の concat で辞書型を追加する方法

Last updated at Posted at 2022-12-01

はじめに

pandas.DataFrame に対して frame.append を行うと pandas1.4 以降では Deprecated となった。

FutureWarning: The frame.append method is deprecated and will be removed from
pandas in a future version. Use pandas.concat instead.

これまでは dict を直接追加するために使っていたが、言われたとおり pandas.cancat で書き直そうとしたがそのまま書き直すだけではうまく行かなかったのでメモを残しておく。

修正例

変更前

additional_dict = dict(...)
df = df.append(additional_dict, ignore_index=True)

変更後

import pandas as pd

df_add = pd.DataFrame(additional_dict, index=[0])
df = pd.concat([df, df_add], ignore_index=True)

コメント

注意点としては

  1. 辞書オブジェクトを pd.DataFrame に変換すること
  2. その際に index をリストで与えること

に注意する必要がある。

以上。

1
0
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
1
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?