'''
【课程2.2】 Pandas数据结构Series:基本概念及创建
"一维数组"Serise
'''
import numpy as np
import pandas as pd
s= pd. Series( np. random. rand( 5 ) )
print ( s)
s= pd. Series( np. array( [ 1 , 2 , 3 , 5 , 4 ] ) )
print ( s)
0 0.597451
1 0.760871
2 0.992611
3 0.861297
4 0.525024
dtype: float64
0 1
1 2
2 3
3 5
4 4
dtype: int32
s= pd. Series( {
'one' : 'a' ,
'two' : 'b' ,
'three' : 'c'
} )
print ( s)
one a
three c
two b
dtype: object
s= pd. Series( {
'one' : { 'a' , 'b' } ,
2 : 'b' ,
'three' : { 'c' , 'd' , 'f' }
} )
print ( s)
2 b
one {a, b}
three {c, d, f}
dtype: object
s= pd. Series( [ 'a' , 'b' , 'c' , 'd' , ] )
print ( s)
0 a
1 b
2 c
3 d
dtype: object
s= pd. Series( 10 , index= list ( 'abcdef' ) )
print ( s)
a 10
b 10
c 10
d 10
e 10
f 10
dtype: int64
s= pd. Series( {
'one' : 'a' ,
'two' : 'b' ,
'three' : 'c'
} )
print ( s)
one a
three c
two b
dtype: object
s= pd. Series( [ 1 , 2 , 3 , 5 ] , name= '数字' )
print ( s)
0 1
1 2
2 3
3 5
Name: 数字, dtype: int64
s= s. rename( '重命名' )
print ( s)
0 1
1 2
2 3
3 5
Name: 重命名, dtype: int64
'''
【课程2.3】 Pandas数据结构Series:索引
位置下标 / 标签索引 / 切片索引 / 布尔型索引
'''
s= pd. Series( np. random. rand( 5 ) * 100 )
print ( s)
0 29.070191
1 22.470569
2 98.036061
3 70.841708
4 19.292308
dtype: float64
print ( s[ 2 ] )
98.0360611027
print ( type ( s[ 3 ] ) )
a= float ( s[ 3 ] )
print ( type ( a) )
<class 'numpy.float64'>
<class 'float'>
print ( s[ : : - 1 ] )
4 19.292308
3 70.841708
2 98.036061
1 22.470569
0 29.070191
dtype: float64
s= pd. Series( np. random. rand( 5 ) ,
index= list ( 'abcde' ) )
print ( s)
a 0.362709
b 0.092404
c 0.965135
d 0.207001
e 0.819382
dtype: float64
print ( s[ 'a' ] )
0.3627090717
print ( s[ [ 'a' , 'c' , 'e' ] ] )
a 0.362709
c 0.965135
e 0.819382
dtype: float64
print ( s[ 1 : 4 ] )
b 0.092404
c 0.965135
d 0.207001
dtype: float64
print ( s[ 'a' : 'd' ] )
a 0.362709
b 0.092404
c 0.965135
d 0.207001
dtype: float64
print ( s[ 2 : ] )
c 0.965135
d 0.207001
e 0.819382
dtype: float64
print ( s[ : 3 ] )
a 0.362709
b 0.092404
c 0.965135
dtype: float64
print ( s[ : : 2 ] )
a 0.362709
c 0.965135
e 0.819382
dtype: float64
s= pd. Series( np. random. rand( 5 ) * 100 )
print ( s)
0 44.252999
1 44.853123
2 52.063017
3 88.140812
4 7.004036
dtype: float64
print ( s> 50 )
0 False
1 False
2 True
3 True
4 False
dtype: bool
print ( s[ s> 50 ] )
2 52.063017
3 88.140812
dtype: float64
'''
【课程2.4】 Pandas数据结构Series:基本技巧
数据查看 / 重新索引 / 对齐 / 添加、修改、删除值
'''
s= pd. Series( np. random. rand( 10 ) )
print ( s)
0 0.476257
1 0.663716
2 0.475116
3 0.962667
4 0.788937
5 0.766607
6 0.434719
7 0.952197
8 0.880334
9 0.204090
dtype: float64
print ( s. head( ) )
0 0.476257
1 0.663716
2 0.475116
3 0.962667
4 0.788937
dtype: float64
print ( s. tail( 3 ) )
7 0.952197
8 0.880334
9 0.204090
dtype: float64
s= pd. Series( np. random. rand( 3 ) , index= list ( 'abd' ) )
print ( s)
a 0.169277
b 0.371673
d 0.794740
dtype: float64
s1= s. reindex( [ 'c' , 'd' , 'e' , 'f' , 'a' ] )
print ( s1)
c NaN
d 0.794740
e NaN
f NaN
a 0.169277
dtype: float64
s2= s. reindex( [ 'c' , 'b' , 'f' , 'a' , 'e' ] , fill_value= 5 )
print ( s2)
c 5.000000
b 0.371673
f 5.000000
a 0.169277
e 5.000000
dtype: float64
s1= pd. Series( np. random. rand( 3 ) , index= list ( 'zbc' ) )
s2= pd. Series( np. random. rand( 3 ) , index= list ( 'bca' ) )
print ( s1)
print ( s2)
z 0.775781
b 0.120130
c 0.027019
dtype: float64
b 0.853151
c 0.688326
a 0.708527
dtype: float64
print ( s1+ s2)
a NaN
b 0.973281
c 0.715345
z NaN
dtype: float64
s= pd. Series( np. random. rand( 5 ) , index= list ( 'abcde' ) )
ss= pd. Series( np. random. rand( 5 ) )
print ( s)
s1= s. drop( 'a' )
print ( s1)
s2= ss. drop( 3 )
print ( s2)
s3= s. drop( [ 'a' , 'c' , 'e' ] )
print ( s3)
a 0.090122
b 0.941888
c 0.636887
d 0.960461
e 0.312361
dtype: float64
b 0.941888
c 0.636887
d 0.960461
e 0.312361
dtype: float64
0 0.644486
1 0.348466
2 0.810254
4 0.624436
dtype: float64
b 0.941888
d 0.960461
dtype: float64
s1= pd. Series( np. random. rand( 3 ) )
s2= pd. Series( np. random. rand( 3 ) , index= list ( 'abc' ) )
print ( s1, s2)
0 0.009789
1 0.629592
2 0.147525
dtype: float64 a 0.018171
b 0.340888
c 0.998620
dtype: float64
s1[ 5 ] = 100
print ( s1)
0 0.009789
1 0.629592
2 0.147525
5 100.000000
dtype: float64
s2[ 'e' ] = 100
print ( s2)
a 0.018171
b 0.340888
c 0.998620
e 100.000000
dtype: float64
s3= s1. append( s2)
print ( s3)
0 0.009789
1 0.629592
2 0.147525
5 100.000000
a 0.018171
b 0.340888
c 0.998620
e 100.000000
dtype: float64
s1= pd. Series( np. random. rand( 3 ) )
s2= pd. Series( np. random. rand( 3 ) , index= list ( 'abc' ) )
print ( s1, s2)
0 0.609883
1 0.970671
2 0.465691
dtype: float64 a 0.212456
b 0.123941
c 0.829763
dtype: float64
s1[ 1 ] = 100
print ( s1)
s1[ [ 1 , 2 ] ] = 200
print ( s1)
0 0.609883
1 100.000000
2 0.465691
dtype: float64
0 0.609883
1 200.000000
2 200.000000
dtype: float64
s2[ [ 'a' , 'b' ] ] = 300
print ( s2)
a 300.000000
b 300.000000
c 0.829763
dtype: float64