pandas.Series学习
#pandas 基础
import numpy as np
import pandas as pd
#Series是一个随机数组
ar=np.random.rand(5)
print (ar)
s=pd.Series(ar)
print (s)
[0.48942827 0.47321657 0.37859367 0.86765122 0.89666973] 0 0.489428 1 0.473217 2 0.378594 3 0.867651 4 0.896670 dtype: float64#name属性
a=pd.Series(np.random.rand(100),name='fasdfdas')
b=pd.Series([90,92,89,65],index=['Jack','Marry','Tom','Zack'],name='作业',dtype=np.float64)
b
Jack 90.0 Marry 92.0 Tom 89.0 Zack 65.0 Name: 作业, dtype: float64#Series索引
s=pd.Series(np.random.rand(10))
print (s)
print (s[5],type(s[5]),s[5].dtype)
0 0.555260 1 0.925801 2 0.354234 3 0.597488 4 0.652729 5 0.216476 6 0.269461 7 0.957974 8 0.625409 9 0.727877 dtype: float64 0.21647554176678407 <class 'numpy.float64'> float64#标签索引
s=pd.Series(np.random.rand(5),index=['a','b','c','d','e'])
print (s)
print (s[['a','b','c']])#注意要选取多个索引记住多加括号,位置自己随意
<div><br class="Apple-interchange-newline">#标签索引
s=pd.Series(np.random.rand(5),index=['a','b','c','d','e'])
print (s)
print (s[['a','b','c']])#注意要选取多个索引记住多加括号,位置自己随意</div>
#标签索引
s=pd.Series(np.random.rand(5),index=['a','b','c','d','e'])
print (s)
print (s[['a','b','c']])#注意要选取多个索引记住多加括号,位置自己随意
s1=pd.Series(np.random.rand(5))
s2=pd.Series(np.random.rand(5),index=['a','b','c','d','e'])
print (s1[1:4])#下标
print (s2['a':'c'])#标签index
#有标签用标签,没标签用下标,用index做切片是末端包含的
1 0.129370 2 0.220584 3 0.144212 dtype: float64 a 0.653526 b 0.153144 c 0.466642 dtype: float64#布尔型索引
s=pd.Series(np.random.rand(3)*100)
s[3]=None
bs1=s>50
bs2=s.isnull()
bs3=s.notnull()
print (bs1)
print (bs2)
print (bs3)
0 True 1 True 2 False 3 False dtype: bool 0 False 1 False 2 False 3 True dtype: bool 0 True 1 True 2 True 3 False dtype: bool#Series 重建索引,对齐,添加,修改,删除
s=pd.Series(np.random.rand(5),index=['a','b','c','d','e'])
print (s)
s1=s.reindex(['c','d','a','f','g'])#保留原有索引,如果有新名称,返回Nan
print (s1)
s2=s.reindex(['c','a','d','f'],fill_value=0)#填充为0
print (s2)
a 0.136703 b 0.735538 c 0.693360 d 0.224563 e 0.523118 dtype: float64 c 0.693360 d 0.224563 a 0.136703 f NaN g NaN dtype: float64 c 0.693360 a 0.136703 d 0.224563 f 0.000000 dtype: float64#对齐
s1=pd.Series(np.random.rand(3),index=['Jack','Marry','Tom'])
s2=pd.Series(np.random.rand(3),index=['Jack','Marry','wang'])
print (s1+s2)
Jack 1.254179 Marry 0.976891 Tom NaN wang NaN dtype: float64#删除和添加
s=pd.Series(np.random.rand(5),index=list('fasde'))
print (s)
s1=s.drop('s')
s2=s.drop(['f','d'])
print (s1)
print (s2)
print (s)
#删除元素之后返回副本(inplace=False),默认是false,即不代替,也就是不改变原值
f 0.294838 a 0.367201 s 0.785893 d 0.395416 e 0.998229 dtype: float64 f 0.294838 a 0.367201 d 0.395416 e 0.998229 dtype: float64 a 0.367201 s 0.785893 e 0.998229 dtype: float64 f 0.294838 a 0.367201 s 0.785893 d 0.395416 e 0.998229 dtype: float64#添加
s1=pd.Series(np.random.rand(5))
s2=pd.Series(np.random.rand(5),index=list('abcde'))
s1[5]=100
s2['f']=100
print (s1)
print (s2)
s3=s1.append(s2)
print (s3)
0 0.596044 1 0.038958 2 0.415344 3 0.412761 4 0.535289 5 100.000000 dtype: float64 a 0.830547 b 0.439642 c 0.867376 d 0.611164 e 0.452300 f 100.000000 dtype: float64 0 0.596044 1 0.038958 2 0.415344 3 0.412761 4 0.535289 5 100.000000 a 0.830547 b 0.439642 c 0.867376 d 0.611164 e 0.452300 f 100.000000 dtype: float64