pandas的Series

206 阅读1分钟

7. 学习 + 练习

import numpy as np
import pandas as pd


# 下标索引
s = pd.Series(np.random.rand(5))
print(s)

print(s[3], type(s[3]), s[3].dtype)
# 0.018210697390008912 <class 'numpy.float64'> float64


# print(s[-1]) 未知索引

# 标签索引  是 a - j
s2 = pd.Series(np.random.rand(5), index=['a', 'b', 'c', 'd', 'e'])  # 标签
print(s2)

print(s2['a'], type(s2['a']), s2['a'].dtype)

print(s2[['a', 'b']])
# 顺序是可以改变的
print(s2[['b', 'a', 'c']])

# 切片  使用index切片是末端包含的
s3 = pd.Series(np.random.rand(5), index=['a', 'b', 'c', 'd', 'e'])
print(s3['a': 'c'])
print(s3['c'])
print('------')
# 切片  末端不包含
s4 = pd.Series(np.random.rand(5))  # 下标
print(s4[1:3], s4[3])

# 布尔型索引
s5 = pd.Series(np.random.rand(3) * 100)
s5[4] = None
print(s5)   # dtype: object

b = s5.isnull()
b1 = s5 > 50
b2 = s5.notnull()

print(b, type(b), b.dtype)
print(b1, type(b1), b1.dtype)
print(b2, type(b2), b2.dtype)
# dtype: bool <class 'pandas.core.series.Series'> bool

# 只返回 True的数据
print(s5[s5 > 50])