'''
【课程2.2】 Pandas数据结构Series:基本概念及创建
"一维数组"Serise
'''
'\n【课程2.2】 Pandas数据结构Series:基本概念及创建\n\n"一维数组"Serise\n\n'
import numpy as np
import pandas as pd
s= pd. Series( np. random. rand( 5 ) )
print ( s)
print ( '------' )
print ( type ( s) )
0 0.830450
1 0.674102
2 0.528299
3 0.150878
4 0.952043
dtype: float64
------
<class 'pandas.core.series.Series'>
dic = {
'a' : 1 , 'b' : 'hello' , 'c' : 3 , 4 : 4 , 5 : 5 }
s= pd. Series( dic)
print ( s)
a 1
b hello
c 3
4 4
5 5
dtype: object
arr= np. random. randn( 5 )
s= pd. Series( arr)
print ( arr)
print ( s)
'''
pd.Series(
data=None,
index=None,
dtype=None,
name=None,
copy=False,
fastpath=False,
)
'''
s= pd. Series( arr, index= list ( 'abcde' ) , dtype= np. str )
print ( s)
[ 0.26383154 0.97382125 0.13994526 -0.60732141 1.32883897]
0 0.263832
1 0.973821
2 0.139945
3 -0.607321
4 1.328839
dtype: float64
a 0.26383153983884505
b 0.9738212455558085
c 0.1399452564766354
d -0.6073214101102407
e 1.3288389721491793
dtype: object
s= pd. Series( 10 , index= range ( 4 ) )
print ( s)
0 10
1 10
2 10
3 10
dtype: int64
s1= pd. Series( np. random. rand( 5 ) )
print ( s1)
print ( '-----' )
s2= pd. Series( np. random. rand( 5 ) , name= "test" )
print ( s2)
print ( s1. name, s2. name, type ( s2. name) )
s3= s2. rename( 'xjxj' )
print ( s3)
print ( s3 is s2)
print ( s3. name, s2. name)
0 0.603552
1 0.007823
2 0.581088
3 0.262479
4 0.366710
dtype: float64
-----
0 0.638098
1 0.012841
2 0.659852
3 0.009916
4 0.444856
Name: test, dtype: float64
None test <class 'str'>
0 0.638098
1 0.012841
2 0.659852
3 0.009916
4 0.444856
Name: xjxj, dtype: float64
False
xjxj test
dic= {
'Jack' : 90.0 , 'Marry' : 92 , "Tom" : 89 , 'Zack' : 65 }
s1= pd. Series( dic, name= "作业1" )
print ( s1)
ar= np. array( ( 90.0 , 92 , 89 , 65 ) )
s2= pd. Series( ar, index= ( 'Jack' , 'Marry' , "Tom" , 'Zack' ) , name= "作业1" )
print ( s2)
Jack 90.0
Marry 92.0
Tom 89.0
Zack 65.0
Name: 作业1, dtype: float64
Jack 90.0
Marry 92.0
Tom 89.0
Zack 65.0
Name: 作业1, dtype: float64
'''
【课程2.3】 Pandas数据结构Series:索引
位置下标 / 标签索引 / 切片索引 / 布尔型索引
'''
'\n【课程2.3】 Pandas数据结构Series:索引\n\n位置下标 / 标签索引 / 切片索引 / 布尔型索引\n\n'
s= pd. Series( np. random. rand( 5 ) )
print ( s[ 0 ] , type ( s[ 0 ] ) , s[ 0 ] . dtype)
print ( float ( s[ 0 ] ) , type ( float ( s[ 0 ] ) ) )
0.6358412386028008 <class 'numpy.float64'> float64
0.6358412386028008 <class 'float'>
s= pd. Series( np. random. rand( 5 ) , index= list ( 'abcde' ) )
print ( s)
print ( s[ 'a' ] , type ( s[ 'a' ] ) , s[ 'a' ] . dtype)
sci= s[ [ 'a' , 'b' , 'c' ] ]
print ( sci, type ( sci) )
a 0.541327
b 0.810801
c 0.296037
d 0.794296
e 0.899370
dtype: float64
0.5413267940720663 <class 'numpy.float64'> float64
a 0.541327
b 0.810801
c 0.296037
dtype: float64 <class 'pandas.core.series.Series'>
s1= pd. Series( np. random. randint( 10 , size= 5 ) )
s2= pd. Series( np. random. randint( 10 , size= 5 ) , index= list ( "abcde" ) )
print ( s1[ 1 : 4 ] , '\n' , s1[ 1 : - 1 ] , '\n' , s1[ 2 ] )
print ( '---' )
print ( s2[ "a" : "b" ] , '\n' , s2[ 'a' ] )
print ( '---' )
print ( s2[ 1 : - 1 ] , '\n' , s2[ 2 ] )
1 3
2 5
3 0
dtype: int32
1 3
2 5
3 0
dtype: int32
5
---
a 5
b 9
dtype: int32
5
---
b 9
c 5
d 2
dtype: int32
5
s= pd. Series( np. random. rand( 3 ) * 100 )
s[ 4 ] = None
print ( s)
bs1 = s > 50
bs2 = s. isnull( )
bs3 = s. notnull( )
print ( bs1, type ( bs1) , bs1. dtype)
print ( bs2, type ( bs2) , bs2. dtype)
print ( bs3, type ( bs3) , bs3. dtype)
print ( '-----' )
print ( s> 50 )
print ( '-----' )
print ( s[ s> 50 ] )
print ( '-----' )
print ( s[ bs3] )
0 12.5679
1 73.037
2 69.7116
4 None
dtype: object
0 False
1 True
2 True
4 False
dtype: bool <class 'pandas.core.series.Series'> bool
0 False
1 False
2 False
4 True
dtype: bool <class 'pandas.core.series.Series'> bool
0 True
1 True
2 True
4 False
dtype: bool <class 'pandas.core.series.Series'> bool
-----
0 False
1 True
2 True
4 False
dtype: bool
-----
1 73.037
2 69.7116
dtype: object
-----
0 12.5679
1 73.037
2 69.7116
dtype: object
s= pd. Series( np. random. rand( 10 ) * 100 , index= list ( 'abcdefghij' ) )
print ( s)
print ( '-------' )
print ( s[ 'b' ] , s[ 'c' ] )
print ( '-------' )
print ( s[ 4 : 7 ] )
print ( s[ [ 4 , 5 , 6 ] ] )
print ( '-------' )
print ( s[ s> 50 ] )
a 47.610866
b 32.879041
c 60.843136
d 25.798653
e 16.734771
f 72.011496
g 13.186102
h 67.730150
i 28.785863
j 82.482446
dtype: float64
-------
32.87904131859861 60.84313579685892
-------
e 16.734771
f 72.011496
g 13.186102
dtype: float64
e 16.734771
f 72.011496
g 13.186102
dtype: float64
-------
c 60.843136
f 72.011496
h 67.730150
j 82.482446
dtype: float64
'''
【课程2.4】 Pandas数据结构Series:基本技巧
数据查看 / 重新索引 / 对齐 / 添加、修改、删除值
'''
'\n【课程2.4】 Pandas数据结构Series:基本技巧\n\n数据查看 / 重新索引 / 对齐 / 添加、修改、删除值\n\n'
s= pd. Series( np. random. rand( 50 ) )
print ( s. head( 10 ) )
print ( s. tail( ) )
0 0.583793
1 0.340821
2 0.153140
3 0.726648
4 0.482695
5 0.652023
6 0.328461
7 0.177034
8 0.217062
9 0.341393
dtype: float64
45 0.425366
46 0.712421
47 0.423743
48 0.980984
49 0.146227
dtype: float64
s= pd. Series( np. random. rand( 3 ) , index= list ( 'abc' ) )
print ( s)
s1= s. reindex( list ( 'cbad' ) )
print ( s1)