pandas dataframe 列 追加

pandasのdataframeに列を追加する方法

column名指定して追加する場合

df[追加column名] = 追加データ

insertを使う場合

df.insert(追加する位置,追加column名,追加データ)

assignを使う場合

df = df.assign(追加column名 = 追加データ)

column名を指定して追加

最終列に追加される

import pandas as pd
import numpy as np

# DataFrame作成
df = pd.DataFrame([[25,'pika','Electric',np.NaN],
                   [131,'rara','Water','Ice'],
                   [143,'gon','Normal',np.NaN],
                   [3,'fushi','Grass','Poison'],
                   [6,'kage','Fire','Flying'],
                   [9,'kame','Water',np.NaN]],
                  columns=['no','name','type1','type2'],
                  index=['party1','party2','party3','party4','party5','party6'])

# 作成したDataFrameの確認
print(df)

"""
         no   name     type1   type2
party1   25   pika  Electric     NaN
party2  131   rara     Water     Ice
party3  143    gon    Normal     NaN
party4    3  fushi     Grass  Poison
party5    6   kage      Fire  Flying
party6    9   kame     Water     NaN
"""

# [level]という名前のカラム(列)追加
df['level'] = [88,80,82,84,84,84]

# 追加後確認
print(df)

"""
         no   name     type1   type2  level
party1   25   pika  Electric     NaN     88
party2  131   rara     Water     Ice     80
party3  143    gon    Normal     NaN     82
party4    3  fushi     Grass  Poison     84
party5    6   kage      Fire  Flying     84
party6    9   kame     Water     NaN     84
"""

追加するデータをリストではなく1つのデータを指定することですべての行に同じデータが入る

# 上記に加えて
# [itemheld]という名前のカラム(列)追加
df['itemheld'] = np.NaN

# 追加後確認
print(df)

"""
         no   name     type1   type2  level  helditem
party1   25   pika  Electric     NaN     88       NaN
party2  131   rara     Water     Ice     80       NaN
party3  143    gon    Normal     NaN     82       NaN
party4    3  fushi     Grass  Poison     84       NaN
party5    6   kage      Fire  Flying     84       NaN
party6    9   kame     Water     NaN     84       NaN
"""

insertを使う

insertを使うと任意の場所(列)に追加することが可能

import pandas as pd
import numpy as np

# DataFrame作成
df = pd.DataFrame([[25,'pika','Electric',np.NaN],
                   [131,'rara','Water','Ice'],
                   [143,'gon','Normal',np.NaN],
                   [3,'fushi','Grass','Poison'],
                   [6,'kage','Fire','Flying'],
                   [9,'kame','Water',np.NaN]],
                  columns=['no','name','type1','type2'],
                  index=['party1','party2','party3','party4','party5','party6'])

# 作成したDataFrameの確認
print(df)

"""
         no   name     type1   type2
party1   25   pika  Electric     NaN
party2  131   rara     Water     Ice
party3  143    gon    Normal     NaN
party4    3  fushi     Grass  Poison
party5    6   kage      Fire  Flying
party6    9   kame     Water     NaN
"""

# [name]と[type1]の間に[level]列を追加
df.insert(2,'level',[88,80,82,84,84,84])

# 追加後確認
print(df)

"""
         no   name  level     type1   type2
party1   25   pika     88  Electric     NaN
party2  131   rara     80     Water     Ice
party3  143    gon     82    Normal     NaN
party4    3  fushi     84     Grass  Poison
party5    6   kage     84      Fire  Flying
party6    9   kame     84     Water     NaN

こちらも追加するデータをリストではなく1つのデータを指定することですべての行に同じデータが入る

# 上記に加えて
# [itemheld]という名前のカラム(列)を最後の列に追加
df.insert(len(df.columns),'itemheld',np.NaN)

# 追加後確認
print(df)

"""
         no   name  level     type1   type2  itemheld
party1   25   pika     88  Electric     NaN       NaN
party2  131   rara     80     Water     Ice       NaN
party3  143    gon     82    Normal     NaN       NaN
party4    3  fushi     84     Grass  Poison       NaN
party5    6   kage     84      Fire  Flying       NaN
party6    9   kame     84     Water     NaN       NaN
"""

assignを使う

最終列に追加される。追加位置の指定ができない。lamda関数が使える。
追加のcolumn名は半角英数しか指定できない。(全角も設定できるかもしれないが方法をしらない)

import pandas as pd
import numpy as np

# DataFrame作成
df = pd.DataFrame([[25,'pika','Electric',np.NaN],
                   [131,'rara','Water','Ice'],
                   [143,'gon','Normal',np.NaN],
                   [3,'fushi','Grass','Poison'],
                   [6,'kage','Fire','Flying'],
                   [9,'kame','Water',np.NaN]],
                  columns=['no','name','type1','type2'],
                  index=['party1','party2','party3','party4','party5','party6'])

# 作成したDataFrameの確認
print(df)

"""
         no   name     type1   type2
party1   25   pika  Electric     NaN
party2  131   rara     Water     Ice
party3  143    gon    Normal     NaN
party4    3  fushi     Grass  Poison
party5    6   kage      Fire  Flying
party6    9   kame     Water     NaN
"""

# [level]という名前のカラム(列)追加
df = df.assign(level = [88,80,82,84,84,84])

# 追加後確認
print(df)

"""
         no   name     type1   type2  level
party1   25   pika  Electric     NaN     88
party2  131   rara     Water     Ice     80
party3  143    gon    Normal     NaN     82
party4    3  fushi     Grass  Poison     84
party5    6   kage      Fire  Flying     84
party6    9   kame     Water     NaN     84
"""

くどいが追加するデータをリストではなく1つのデータを指定することですべての行に同じデータが入る

# 上記に加えて
# [itemheld]という名前のカラム(列)を最後の列に追加
df = df.assign(itemheld = np.NaN)

# 追加後確認
print(df)

"""
         no   name     type1   type2  level  helditem
party1   25   pika  Electric     NaN     88       NaN
party2  131   rara     Water     Ice     80       NaN
party3  143    gon    Normal     NaN     82       NaN
party4    3  fushi     Grass  Poison     84       NaN
party5    6   kage      Fire  Flying     84       NaN
party6    9   kame     Water     NaN     84       NaN
"""

コメント

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