2
1

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.

Series 要素の追加・削除

Posted at

#要素の追加
Seriesに要素を追加する時、追加する要素もSeries型である必要がある。
追加したい要素をSeries型に変換後、追加先に**append(データ配列, index=インデックス配列 )**で渡す事が可能。

import pandas as pd

fruits = ["apple", "orange", "banana", "straberry", "kiwifruit"]
data = [10, 5, 1, 2, 3]

#Series型の作成
series = pd.Series(data, index = fruits)
print(series)

#データ値が12,インデックスがpineappleのSeries型に変換し、appendで追加
series = series.append(pd.Series([12], index = ["pineapple"]))

print(series)

###出力結果
image.png

一番最後にpineapple 12が追加されました。

#要素の削除
変数に対して、 **.drop("インデックス")**とする事で指定のインデックスを持つ要素が削除できる。

#上記のSeries型のappleを削除する。
series = series.drop("apple")
print(series)

###出力結果
image.png

見事appleが削除されました。

2
1
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
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?