pandas.DataFrameのカラム追加・削除(assign・drop)

当ページではpandas.DataFrameのデータの検索方法を記載している。
pandas.DataFrameのデータを作成する方法は、別途以下で記載している。
pandas.DataFrameの表を作成する方法 | エクヌツITブログ
本ページでは、以下のデータをベースのデータとして扱い、
そのデータが各作業でどのようになるかを説明する。

import pandas as pd
import numpy as np

df = pd.DataFrame(np.array([[1, 2, 3], [6, 5, 4], [7, 8, 9]]),
                   columns=['a', 'b', 'c'],index=['d', 'e', 'f'])
print(df)

動作結果:

結果:
   a  b  c
d  1  2  3
e  6  5  4
f  7  8  9

1.カラムの追加

カラムの追加は目的によっていくつかのパターンがある。
当ページでは以下の作業の方法を記載している。
(a)新しいカラムを作成して、それぞれに異なる値を設定する
(b)新しいカラムを作成して、同一の値を追加する
(c)新しいカラムを作成して、別カラムの値を参考に値を追加する

(a)新しいカラムを作成して、それぞれに異なる値を設定する

新しいカラムを作成してそれぞれに異なる値を設定する場合、以下のように新しいカラム名を指定して、値を設定することで追加する事ができる。

import pandas as pd
import numpy as np

df = pd.DataFrame(np.array([[1, 2, 3], [6, 5, 4], [7, 7, 7]]),
                   columns=['a', 'b', 'c'],index=['d', 'e', 'f'])
print("初期データ") 
print(df) 
print("新しいカラムにX・Y・Zを設定して作成")
df["New1"]=["X","Y","Z"]
print(df) 

実行結果:

実行結果:
初期データ
   a  b  c
d  1  2  3
e  6  5  4
f  7  7  7
新しいカラムにX・Y・Zを設定して作成
   a  b  c New1
d  1  2  3    X
e  6  5  4    Y
f  7  7  7    Z

上記の例の場合は、10行目で[NEW1]というカラムを初期値[X,Y,Z]で作成している。

(b)新しいカラムを作成して、同一の値を追加する

新しいカラムを作成して全て同じ値を設定する場合、assign関数を使用することで容易に設定できる。

import pandas as pd
import numpy as np

df = pd.DataFrame(np.array([[1, 2, 3], [6, 5, 4], [7, 7, 7]]),
                   columns=['a', 'b', 'c'],index=['d', 'e', 'f'])
print("初期データ") 
print(df) 
print("新しい値に全て0を設定。")
df=df.assign(New2=0)
print(df) 

実行結果:

初期データ
   a  b  c
d  1  2  3
e  6  5  4
f  7  7  7
新しい値に全て0を設定。
   a  b  c  New2
d  1  2  3     0
e  6  5  4     0
f  7  7  7     0

(c)新しいカラムを作成して、別カラムの値を参考に値を追加する

新しいカラムを作成して別カラムの値を参考に値を追加する場合、assign関数を使用することで設定できる。

import pandas as pd
import numpy as np

df = pd.DataFrame(np.array([[1, 2, 3], [6, 5, 4], [7, 7, 7]]),
                   columns=['a', 'b', 'c'],index=['d', 'e', 'f'])
print("初期データ") 
print(df) 
print("新しい値にカラムaに2を掛けた数を設定。")
df=df.assign(New3=df["a"] * 2)
print(df) 

実行結果:

初期データ
   a  b  c
d  1  2  3
e  6  5  4
f  7  7  7
新しい値にカラムaに2を掛けた数を設定。
   a  b  c  New3
d  1  2  3     2
e  6  5  4    12
f  7  7  7    14

上記の例の場合は、10行目で[NEW3]というカラムを[a]を2倍した値を初期値として設定している。

2.カラムの削除

カラムの削除はdropを使用して行う。

import pandas as pd
import numpy as np

df = pd.DataFrame(np.array([[1, 2, 3], [6, 5, 4], [7, 7, 7]]),
                   columns=['a', 'b', 'c'],index=['d', 'e', 'f'])
print("初期データ") 
print(df) 
print("aカラムとbカラムを削除する")
df=df.drop(columns=['a', 'b'])
print(df) 

実行結果:

初期データ
   a  b  c
d  1  2  3
e  6  5  4
f  7  7  7
aカラムとbカラムを削除する
   c
d  3
e  4
f  7

上記の例の場合は、9行目でカラム[a]とカラム[b]を削除し、10行目で削除したデータを表示できる。

3.参考

公式ドキュメント:
DataFrameTop
https://pandas.pydata.org/pandas-docs/version/2.3/reference/api/pandas.DataFrame.html
assign
https://pandas.pydata.org/pandas-docs/version/2.3/reference/api/pandas.DataFrame.assign.html

表の作成
pandas.DataFrameの表を作成する方法 | エクヌツITブログ
pandas値の取得・更新・削除
pandas.DataFrameの値の取得・更新・削除 | エクヌツITブログ
pandasのデータの検索
pandas.DataFlameのデータ検索(loc,iloc,query) | エクヌツITブログ
pandasのインポート
pandasのインポートread_csv,read_excel | エクヌツITブログ
pandasのエクスポート
pandasデータのエクスポートto_csv,to_excel | エクヌツITブログ
pandasの要素数取得
pandas.DataFrame要素数を取得size,count | エクヌツITブログ

コメント

タイトルとURLをコピーしました