Warning: Undefined variable $position in /home/pystyles/pystyle.info/public_html/wp/wp-content/themes/lionblog/functions.php on line 4897

pandas – Series のインデックス操作まとめ

pandas – Series のインデックス操作まとめ

概要

Series の値を取得、設定する方法について解説します。

関連記事 pandas – DataFrame のインデックス操作まとめ – pystyle

Advertisement

Series のインデックスを取得する

pandas.Series.index または pandas.Series.keys() で Series のインデックスを取得できます。また、Series を iterate すると、インデックスが返ります。

In [1]:
import pandas as pd

s = pd.Series([11, 12, 13, 14, 15], index=["a", "b", "c", "d", "e"])
print(s)
a    11
b    12
c    13
d    14
e    15
dtype: int64
In [2]:
print(s.index)
print(s.keys())

for i in s:
    print(i, end=" ")
Index(['a', 'b', 'c', 'd', 'e'], dtype='object')
Index(['a', 'b', 'c', 'd', 'e'], dtype='object')
11 12 13 14 15 

Series の要素を1つずつ iterate する

pandas.Series.iteritems() 及び pandas.Series.items() で、(インデックス, 値) を返す iterable オブジェクトを取得できます。

In [3]:
print(type(s.iteritems()))
for i, v in s.iteritems():
    print(i, v)

print(type(s.items()))
for i, v in s.items():
    print(i, v)
<class 'zip'>
a 11
b 12
c 13
d 14
e 15
<class 'zip'>
a 11
b 12
c 13
d 14
e 15

Series の選択した範囲の値を取得または設定する

範囲の選択方法はインデックスに基づく選択と位置に基づく選択があります。

atloc__getitem__()iatiloc は選択範囲に値を設定することもできます。

In [4]:
s2 = s.copy()
s2.at["b"] = 100
print(s2)

s2 = s.copy()
s2["b":"d"] = 100  # または s.loc["b":"d"] = 100
print(s2)
a     11
b    100
c     13
d     14
e     15
dtype: int64
a     11
b    100
c    100
d    100
e     15
dtype: int64
In [5]:
s2 = s.copy()
s2.iat[1] = 100
print(s2)

s2 = s.copy()
s2.iloc[1:3] = 100
print(s2)
a     11
b    100
c     13
d     14
e     15
dtype: int64
a     11
b    100
c    100
d     14
e     15
dtype: int64

インデックスに基づく選択

取得 代入 Indexing Slice Bool Indexing Fancy Indexing
pandas.Series.at s.at[index]
pandas.Series.get() s.get(index)
pandas.Series.xs() s.xs(index)
pandas.Series.loc s.loc[index] s.loc[slice] s.loc[bools] s.loc[indices]
__getitem__() s[index] s[slice] s[bools] s[indices]

Indexing

In [6]:
print(s["b"])  # または s.at["b"]、s.get("b")、s.xs("b")、s.loc["b"]
12

get() は指定したインデックスが存在しない場合は None を返します。存在しない場合に返す値を default に指定できます。

In [7]:
print(s.get("f"))
print(s.get("f", default=-1))
None
-1

Slice

list のスライスと異なり、インデックスのスライスは終端も含みます

In [8]:
print(s["b":])  # または s.loc["b":]
print(s[:"d"])  # または s.loc[:"d"]
print(s["b":"d"])  # または s.loc["b":"d"]
print(s[::2])  # または s.loc[::2]
b    12
c    13
d    14
e    15
dtype: int64
a    11
b    12
c    13
d    14
dtype: int64
b    12
c    13
d    14
dtype: int64
a    11
c    13
e    15
dtype: int64

Bool Indexing

In [9]:
print(s[[True, False, True, False, True]])
# または s.loc[[True, False, True, False, True]]
a    11
c    13
e    15
dtype: int64

Fancy Indexing

In [10]:
print(s[["a", "c", "e"]])
# または s.loc[["a", "c", "e"]]
a    11
c    13
e    15
dtype: int64
Advertisement

位置に基づく選択

  取得 代入 Indexing Slice Fancy Indexing
pandas.Series.iat s.iat[position]    
pandas.Series.take()     s.take(positions)
pandas.Series.iloc s.iloc[position] s.iloc[slice] s.iloc[positions]

Indxing

In [11]:
print(s.iat[1])
12

Slice

list のスライスと同様、位置のスライスは終端は含みません

In [12]:
print(s.iloc[1:])
print(s.iloc[:3])
print(s.iloc[1:3])
print(s.iloc[::2])
b    12
c    13
d    14
e    15
dtype: int64
a    11
b    12
c    13
dtype: int64
b    12
c    13
dtype: int64
a    11
c    13
e    15
dtype: int64

Fancy Indexng

In [13]:
print(s.iloc[[0, 2, 4]])
print(s.take([0, 2, 4]))
a    11
c    13
e    15
dtype: int64
a    11
c    13
e    15
dtype: int64