1. 程式人生 > 實用技巧 >Pandas的Series常用方法

Pandas的Series常用方法

使用

from pandas import Series

1. 建立Series

a. 常規建立

>>> obj = Series([1,2,3], index=['A','B','C'])
>>> obj
A    1
B    2
C    3
dtype: int64

b. 根據字典建立

>>> obj = Series({'a':1,'b':2,'c':3})
>>> obj
a    1
b    2
c    3
dtype: int64

c. Series巢狀Series

>>> obj1 = Series([1,2,3],index=['a','b','c'])
>>> obj2 = Series([4,5,6],index=['d','e','f'])
>>> obj3 = Series([obj1, obj2],index=['name1', 'name2'])
>>> obj3
name1    a    1
b    2
c    3
dtype: int64
name2    d    4
e    5
f    6
dtype: int64
dtype: object

2. Series追加

>>> obj1 = Series([1,2,3],index=['a','b','c'])
>>> obj1
a    1
b    2
c    3
dtype: int64
>>> obj1.append(Series([4,5],index=['d','e']))
a    1
b    2
c    3
d    4
e    5
dtype: int64

3. Series刪除

>>> obj1 = Series([1,2,3],index=['a','b','c'])
>>> obj1
a    1
b    2
c    3
dtype: int64
>>> obj1.drop('b')
a    1
c    3
dtype: int64

4. Series改

>>> obj1 = Series([1,2,3],index=['a','b','c'])
>>> obj1
a    1
b    2
c    3
dtype: int64
>>> obj1.a = -1
>>> obj1['b'] = -2
>>> obj1
a   -1
b   -2
c    3
dtype: int64

5. Series查

>>> obj1 = Series([1,2,3],index=['a','b','c'])
>>> obj1
a    1
b    2
c    3
dtype: int64
>>> print(obj1.a == 1)
True