【1.4】pandas学习—Series索引

版权声明:版权所有,翻版必究【Kevin】 https://blog.csdn.net/weixin_30935137/article/details/81210409

位置下标

import numpy as np
import pandas as pd

s = pd.Series(np.random.rand(5))
print(s)
print('-------------------------')
print(s[0],type(s[0]),s[0].dtype)
print('-------------------------')
print(float(s[0]),type(float(s[0])))
# 位置下标从0开始
# 输出结果是numpy.float格式
# 可以通过float函数()转换为python float格式
# numpy.float与float占用的字节不同

# print(s[-1]) 会报错

结果显示如下:

0    0.793525
1    0.897458
2    0.671203
3    0.484800
4    0.254393
dtype: float64
-------------------------
0.793525471568 <class 'numpy.float64'> float64
-------------------------
0.7935254715678627 <class 'float'>

标签索引

s = pd.Series(np.random.rand(5),index = ['a','b','c','d','e'])
print(s)
print('---------------------------')
print(s['a'],type(s['a']),s['a'].dtype)
print('---------------------------')
# 方法类似下标索引,用[]表示,内写上index,注意index是字符串

sci = s[['a','b','e']]
print(sci,type(sci))
# 如果需要选择多个标签的值,用[[]]表示(相当于[]中包含一个列表)
# 多标签索引的结果是新的数组

结果如下:

a    0.530563
b    0.770618
c    0.068068
d    0.314152
e    0.390095
dtype: float64
---------------------------
0.53056345492 <class 'numpy.float64'> float64
---------------------------
a    0.530563
b    0.770618
e    0.390095
dtype: float64 <class 'pandas.core.series.Series'>

切片索引

s1 = pd.Series(np.random.rand(5))
s2 = pd.Series(np.random.rand(5),index=['a','b','c','d','e'])
print(s1)
print('s1##########################')
print(s2)
print('s2##########################')
print(s1[1:4],s1[4])
print('3##########################')
print(s2['a':'c'],s2['c'])
print(s2[0:3],s2[3])
print('4##########################')
# 注意:用index做切片是末端包含
print('5##########################')
print(s2[:-1])
print(s2[::2])

结果如下:

0    0.966484
1    0.030170
2    0.676669
3    0.222440
4    0.975929
dtype: float64
s1##########################
a    0.490131
b    0.588430
c    0.835201
d    0.094572
e    0.299277
dtype: float64
s2##########################
1    0.030170
2    0.676669
3    0.222440
dtype: float64 0.975929266797
3##########################
a    0.490131
b    0.588430
c    0.835201
dtype: float64 0.835200632636
a    0.490131
b    0.588430
c    0.835201
dtype: float64 0.0945722312415
4##########################
5##########################
a    0.490131
b    0.588430
c    0.835201
d    0.094572
dtype: float64
a    0.490131
c    0.835201
e    0.299277
dtype: float64

布尔型索引

s = pd.Series(np.random.rand(3)*100)
s[4] = None
print(s)
print('---------------------------')
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)
# 数组做判断之后,返回的是一个由布尔值组成的新的数组
# .isnull()/.notnull()判断是否为空值(None代表空值,NaN代表有问题的数值,两个都会识别为空值)
print('---------------------------')
print(s[s>50])
print(s[bs3])
# 布尔型索引方法,用[判断条件]表示,其中判断条件可以是一个语句或者 一个布尔型数组

结果如下:

0    17.5257
1    1.29052
2    6.12439
4       None
dtype: object
---------------------------
0    False
1    False
2    False
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
---------------------------
Series([], dtype: object)
0    17.5257
1    1.29052
2    6.12439
dtype: object

猜你喜欢

转载自blog.csdn.net/weixin_30935137/article/details/81210409