概要
DataFrame 及び Series に値を追加する append、insert、assign について解説します。
関数一覧
| DataFrame | Series |
|---|---|
| pandas.DataFrame.append | pandas.Series.append |
| pandas.DataFrame.insert | |
| pandas.DataFrame.pop | pandas.Series.pop |
| pandas.DataFrame.assign |
pandas.DataFrame.append – DataFrame に行を追加する
pandas.DataFrame.append は、DataFrame に行を追加します。
DataFrame.append(self, other, ignore_index=False, verify_integrity=False, sort=False) → ’DataFrame’
| 名前 | 型 | デフォルト値 |
|---|---|---|
| other | DataFrame or Series/dict-like object、list of these | |
| 追加するデータ | ||
| ignore_index | bool | False |
| True の場合は index ラベルは使用しない | ||
| verify_integrity | bool | False |
| True の場合は index ラベルが重複する場合は ValueError を送出する | ||
| sort | bool | False |
| True の場合は、行を追加後に列をソートする | ||
| 名前 | 説明 |
|---|---|
| DataFrame | 行を追加した DataFrame |
サンプルコード
import pandas as pd
df = pd.DataFrame({"A": [1, 2, 3], "B": [1, 2, 3]})
df
| A | B | |
|---|---|---|
| 0 | 1 | 1 |
| 1 | 2 | 2 |
| 2 | 3 | 3 |
DataFrame + DataFrame
df1.append(df2) の場合、df1 と df2 の columns ラベルで揃えて、df1 の末尾に df2 を追加します。
df2 = pd.DataFrame({"A": [4, 5, 6], "B": [4, 5, 6]})
ret = df.append(df2)
ret
| A | B | |
|---|---|---|
| 0 | 1 | 1 |
| 1 | 2 | 2 |
| 2 | 3 | 3 |
| 0 | 4 | 4 |
| 1 | 5 | 5 |
| 2 | 6 | 6 |
df1 にない columns ラベルが df2 にある場合は、新しい columns ラベルが追加します。
df2 = pd.DataFrame({"A": [4, 5, 6], "C": [4, 5, 6]})
ret = df.append(df2)
ret
| A | B | C | |
|---|---|---|---|
| 0 | 1 | 1.0 | NaN |
| 1 | 2 | 2.0 | NaN |
| 2 | 3 | 3.0 | NaN |
| 0 | 4 | NaN | 4.0 |
| 1 | 5 | NaN | 5.0 |
| 2 | 6 | NaN | 6.0 |
DataFrame + Series / list of Series
df1.append(s) の場合、df1 の columns ラベルと s の index ラベルで揃えて、df1 の末尾に s2 を追加します。
s = pd.Series([4, 4], index=["A", "B"])
ret = df.append(s, ignore_index=True)
ret
| A | B | |
|---|---|---|
| 0 | 1 | 1 |
| 1 | 2 | 2 |
| 2 | 3 | 3 |
| 3 | 4 | 4 |
s = [pd.Series([4, 4], index=["A", "B"]), pd.Series([5, 5], index=["A", "B"])]
ret = df.append(s, ignore_index=True)
ret
| A | B | |
|---|---|---|
| 0 | 1 | 1 |
| 1 | 2 | 2 |
| 2 | 3 | 3 |
| 3 | 4 | 4 |
| 4 | 5 | 5 |
DataFrame + dict / list of dict
df1.append(d) の場合、df1 の columns ラベルと d の key で揃えて、df1 の末尾に d を追加します。
d = {"A": 4, "B": 4}
ret = df.append(d, ignore_index=True)
ret
| A | B | |
|---|---|---|
| 0 | 1 | 1 |
| 1 | 2 | 2 |
| 2 | 3 | 3 |
| 3 | 4 | 4 |
d = [{"A": 4, "B": 4}, {"A": 5, "B": 5}]
ret = df.append(d, ignore_index=True)
ret
| A | B | |
|---|---|---|
| 0 | 1 | 1 |
| 1 | 2 | 2 |
| 2 | 3 | 3 |
| 3 | 4 | 4 |
| 4 | 5 | 5 |
ignore_index
ignore_index=True の場合、追加後は新たに index ラベルを振り直します。
df2 = pd.DataFrame({"A": [4, 5, 6], "B": [4, 5, 6]})
ret = df.append(df2, ignore_index=True)
ret
| A | B | |
|---|---|---|
| 0 | 1 | 1 |
| 1 | 2 | 2 |
| 2 | 3 | 3 |
| 3 | 4 | 4 |
| 4 | 5 | 5 |
| 5 | 6 | 6 |
sort
sort=True の場合、追加後に列をソートします。
df2 = pd.DataFrame({"0": [4, 5, 6], "1": [4, 5, 6]})
ret = df.append(df2, sort=False)
ret
| A | B | 0 | 1 | |
|---|---|---|---|---|
| 0 | 1.0 | 1.0 | NaN | NaN |
| 1 | 2.0 | 2.0 | NaN | NaN |
| 2 | 3.0 | 3.0 | NaN | NaN |
| 0 | NaN | NaN | 4.0 | 4.0 |
| 1 | NaN | NaN | 5.0 | 5.0 |
| 2 | NaN | NaN | 6.0 | 6.0 |
df2 = pd.DataFrame({"0": [4, 5, 6], "1": [4, 5, 6]})
ret = df.append(df2, sort=True)
ret
| 0 | 1 | A | B | |
|---|---|---|---|---|
| 0 | NaN | NaN | 1.0 | 1.0 |
| 1 | NaN | NaN | 2.0 | 2.0 |
| 2 | NaN | NaN | 3.0 | 3.0 |
| 0 | 4.0 | 4.0 | NaN | NaN |
| 1 | 5.0 | 5.0 | NaN | NaN |
| 2 | 6.0 | 6.0 | NaN | NaN |
pandas.DataFrame.insert – DataFrame に列を挿入する
pandas.DataFrame.insert は、DataFrame の指定した位置に列を挿入します。処理は inplace で行われます。
DataFrame.insert(self, loc, column, value, allow_duplicates=False) →
| 名前 | 型 | デフォルト値 |
|---|---|---|
| loc | int | |
| 列を挿入する位置。0 <= loc <= len(columns) の範囲で指定。 | ||
| column | str, number, or hashable object | |
| 挿入する列の column ラベル | ||
| value | int, Series, or array-like | |
| 挿入する値 | ||
| allow_duplicates | bool, optional | |
| column ラベルが重複するのを許可する | ||
サンプルコード
loc – 列を挿入する位置
指定した位置の直前に挿入します。[0, len(df.columns)] の範囲で指定します。
df = pd.DataFrame({"A": [1, 2, 3], "B": [1, 2, 3]})
df.insert(0, "C", 7)
df
| C | A | B | |
|---|---|---|---|
| 0 | 7 | 1 | 1 |
| 1 | 7 | 2 | 2 |
| 2 | 7 | 3 | 3 |
df = pd.DataFrame({"A": [1, 2, 3], "B": [1, 2, 3]})
df.insert(2, "C", 7)
df
| A | B | C | |
|---|---|---|---|
| 0 | 1 | 1 | 7 |
| 1 | 2 | 2 | 7 |
| 2 | 3 | 3 | 7 |
pandas.DataFrame.pop
pandas.DataFrame.pop は、指定した column ラベルの列を DataFrame から取り出します。取り出した列は DataFrame から削除されます。処理は inplace で行われます。
DataFrame.pop(self: ~ FrameOrSeries, item) → ~FrameOrSeries
| 名前 | 型 | デフォルト値 |
|---|---|---|
| item | str | |
| pop する column ラベル | ||
| 名前 | 説明 |
|---|---|
| Series | pop 後の DataFrame |
サンプルコード
df = pd.DataFrame(
{
"name": ["falcon", "parrot", "lion", "monkey"],
"class": ["bird", "bird", "mammal", "mammal"],
"max_speed": [389.0, 24.0, 80.5, 20.1],
}
)
df
| name | class | max_speed | |
|---|---|---|---|
| 0 | falcon | bird | 389.0 |
| 1 | parrot | bird | 24.0 |
| 2 | lion | mammal | 80.5 |
| 3 | monkey | mammal | 20.1 |
s = df.pop("class")
print(s)
df
0 bird 1 bird 2 mammal 3 mammal Name: class, dtype: object
| name | max_speed | |
|---|---|---|
| 0 | falcon | 389.0 |
| 1 | parrot | 24.0 |
| 2 | lion | 80.5 |
| 3 | monkey | 20.1 |
pandas.DataFrame.assign
pandas.DataFrame.assign は、DataFrame の指定した column ラベルの列に値を代入します。既存の列の場合は上書き、そうでない場合は末尾に挿入されます。
DataFrame.assign(self, **kwargs) → ’DataFrame’
| 名前 | 型 | デフォルト値 |
|---|---|---|
| **kwargs | dict of {str: callable or Series} | |
キーワード引数で column ラベルを指定する。
|
||
| 名前 | 説明 |
|---|---|
| DataFrame | 値を代入した DataFrame |
サンプルコード
df = pd.DataFrame({"temp_c": [17.0, 25.0]}, index=["Portland", "Berkeley"])
df
| temp_c | |
|---|---|
| Portland | 17.0 |
| Berkeley | 25.0 |
callable オブジェクトの場合、DataFrame を受け取り、代入する値を返す関数を指定します。
df2 = df.assign(temp_f=lambda x: x["temp_c"] * 9 / 5 + 32)
df2
| temp_c | temp_f | |
|---|---|---|
| Portland | 17.0 | 62.6 |
| Berkeley | 25.0 | 77.0 |
callable でないオブジェクトの場合、そのまま代入されます。
new_val = df["temp_c"] * 9 / 5 + 32
df2 = df.assign(temp_f=new_val)
df2
| temp_c | temp_f | |
|---|---|---|
| Portland | 17.0 | 62.6 |
| Berkeley | 25.0 | 77.0 |
pandas.Series.append
pandas.Series.append は、Series の末尾に指定した値を追加した Series を返します。
Series.append(self, to_append, ignore_index=False, verify_integrity=False)
| 名前 | 型 | デフォルト値 |
|---|---|---|
| to_append | Series or list/tuple of Series | |
| 追加する値 | ||
| ignore_index | bool | False |
| index ラベルを使用しない。 | ||
| verify_integrity | bool | False |
| True の場合、index ラベルが重複する場合は ValueError 例外を送出する。 | ||
| 名前 | 説明 |
|---|---|
| Series | 追加後の Series |
サンプルコード
Series + Series
s1 = pd.Series([1, 2, 3])
s2 = pd.Series([4, 5, 6])
s = s1.append(s2)
print(s)
0 1 1 2 2 3 0 4 1 5 2 6 dtype: int64
Series + list/tuple of Series
s1 = pd.Series([1, 2, 3])
s2 = [pd.Series([4, 5, 6]), pd.Series([7, 8, 9])]
s = s1.append(s2)
print(s)
0 1 1 2 2 3 0 4 1 5 2 6 0 7 1 8 2 9 dtype: int64
pandas.Series.pop
pandas.Series.pop は、Series から値を1つ取り出し、返します。取り出された値は Series から削除されます。処理は inplace で行われます。
Series.pop(self: ~ FrameOrSeries, item) → ~FrameOrSeries
| 名前 | 型 | デフォルト値 |
|---|---|---|
| item | str | |
| 取り出す index ラベル | ||
| 名前 | 説明 |
|---|---|
| Series | 取り出した値 |
サンプルコード
s1 = pd.Series([1, 2, 3])
print(s1)
0 1 1 2 2 3 dtype: int64
print(s1.pop(1))
print(s1)
2 0 1 2 3 dtype: int64

コメント