Series
Series创建
import pandas as pd
import numpy as np
s1 = pd.Series([1,2,3,4,5])
s1
0 1
1 2
2 3
3 4
4 5
dtype: int64
type(s1)
pandas.core.series.Series
arr1 = np.arange(1,6)
print(arr1)
[1 2 3 4 5]
s2 = pd.Series(arr1)
s2
0 1
1 2
2 3
3 4
4 5
dtype: int32
s2 = pd.Series(arr1,index=['a','b','c','d','e'])
s2
a 1
b 2
c 3
d 4
e 5
dtype: int32
s1.values
array([1, 2, 3, 4, 5], dtype=int64)
s1.index
RangeIndex(start=0, stop=5, step=1)
dict = {'name':'李宁','age':18,'class':'三班'}
s3 = pd.Series(dict,index = ['name','age','class','sex'])
s3
name 李宁
age 18
class 三班
sex NaN
dtype: object
三.Series的基本用法
s3.isnull()
name False
age False
class False
sex True
dtype: bool
s3.notnull()
name True
age True
class True
sex False
dtype: bool
print(s3.index)
print(s3.values)
Index(['name', 'age', 'class', 'sex'], dtype='object')
['李宁' 18 '三班' nan]
s3[0]
'李宁'
s3['age']
18
s3[['name','age']]
name 李宁
age 18
dtype: object
s3[1:3]
age 18
class 三班
dtype: object
s3['name':'class']
name 李宁
age 18
class 三班
dtype: object
s2[s2>3]
d 4
e 5
dtype: int32
print(s2+2)
print(s2>2)
year
a 3
b 4
c 5
d 6
e 7
Name: temp, dtype: int32
year
a False
b False
c True
d True
e True
Name: temp, dtype: bool
s2.name = 'temp'
s2.index.name = 'year'
s2
year
a 1
b 2
c 3
d 4
e 5
Name: temp, dtype: int32
s2.head(3)
year
a 1
b 2
c 3
Name: temp, dtype: int32
s2.tail(2)
year
d 4
e 5
Name: temp, dtype: int32
|