Pandas数据结构Series:基本概念及创建
"一维数组"Serise
`
Series 数据结构
Series 是带有标签的一维数组,可以保存任何数据类型(整数,字符串,浮点数,Python对象等),轴标签统称为索引
导入numpy、pandas模块
import numpy as np
import pandas as pd
s = pd.Series(np.random.rand(6))
查看数据、数据类型
print(1,'-'30)
print(s)
print(2,'-'30)
print(type(s))
.index查看series索引,类型为rangeindex
.values查看series值,类型是ndarray
核心:series相比于ndarray,是一个自带索引index的数组 → 一维数组 + 对应索引
所以当只看series的值的时候,就是一个ndarray
series和ndarray较相似,索引切片功能差别不大
series和dict相比,series更像一个有顺序的字典(dict本身不存在顺序),其索引原理与字典相似(一个用key,一个用index)
print(3,'-'30)
print(s.index,type(s.index))
print(4,'-'30)
print(s.values,type(s.values))
print(5,'-'*30)
print(s.values[3],type(s.values[3]))
运行结果
1 ------------------------------
0 0.175871
1 0.932378
2 0.285359
3 0.566116
4 0.173775
5 0.143258
dtype: float64
2 ------------------------------
<class 'pandas.core.series.Series'>
3 ------------------------------
RangeIndex(start=0, stop=6, step=1) <class 'pandas.core.indexes.range.RangeIndex'>
4 ------------------------------
[0.1758707 0.93237793 0.2853594 0.5661159 0.17377525 0.14325832] <class 'numpy.ndarray'>
5 ------------------------------
0.5661159002308521 <class 'numpy.float64'>
`
# Series 创建方法一:由字典创建,字典的key就是index,values就是values
dic_1 = {'a':1 ,'b':2 , 'c':3, '4':4, '5':5}
s_1 = pd.Series(dic_1)
print(1,'-'*30)
print(s_1)
dic_2 = {'a':1 ,'b':2 , 'c':3, '4':4.0, '5':5}
s_2 = pd.Series(dic_2)
print(2,'-'*30)
print(s_2)
dic_3 = {'a':1 ,'b':'hello' , 'c':3, '4':4, '5':5}
s_3 = pd.Series(dic_3)
print(3,'-'*30)
print(s_3)
# 注意:key肯定是字符串,假如values类型不止一个会怎么样? → dic = {'a':1 ,'b':'hello' , 'c':3, '4':4, '5':5}
#执行结果
1 ------------------------------
a 1
b 2
c 3
4 4
5 5
dtype: int64
2 ------------------------------
a 1.0
b 2.0
c 3.0
4 4.0
5 5.0
dtype: float64
3 ------------------------------
a 1
b hello
c 3
4 4
5 5
dtype: object
# Series 创建方法二:由数组创建(一维数组)
arr = np.random.randn(4)
s = pd.Series(arr)
print(1,'-'*30)
print(arr)
print(2,'-'*30)
print(s)
# 默认index是从0开始,步长为1的数字
s = pd.Series(arr, index = ['a','b','c','d'],dtype = np.object)
print(3,'-'*30)
print(s)
# index参数:设置index,长度保持一致
# dtype参数:设置数值类型
#运行结果
1 ------------------------------
[-0.80928727 0.43796425 -0.43463228 -1.04496999]
2 ------------------------------
0 -0.809287
1 0.437964
2 -0.434632
3 -1.044970
dtype: float64
3 ------------------------------
a -0.809287
b 0.437964
c -0.434632
d -1.04497
dtype: object
# Series 创建方法三:由标量创建
s = pd.Series(10, index = range(1,7))
print(s)
# 如果data是标量值,则必须提供索引。该值会重复,来匹配索引的长度
#运行结果
1 10
2 10
3 10
4 10
5 10
6 10
dtype: int64
# Series 名称属性:name
s1 = pd.Series(np.random.randn(5),index=range(1,6))
print(1,'-'*30)
print(s1)
s2 = pd.Series(np.random.randn(5),name = 'test')
print(2,'-'*30)
print(s2)
print(3,'-'*30)
print(s1.name, s2.name,type(s2.name))
# name为Series的一个参数,创建一个数组的 名称
# .name方法:输出数组的名称,输出格式为str,如果没用定义输出名称,输出为None
s3 = s2.rename('rename_test')
print(4,'-'*30)
print(s3)
print(5,'-'*30)
print(s3.name, s2.name)
# .rename()重命名一个数组的名称,并且新指向一个数组,原数组不变
#执行结果
1 ------------------------------
1 0.683608
2 -0.329336
3 -0.020329
4 -0.169462
5 1.509744
dtype: float64
2 ------------------------------
0 0.214686
1 0.119231
2 -1.665089
3 -0.099371
4 1.048900
Name: test, dtype: float64
3 ------------------------------
None test <class 'str'>
4 ------------------------------
0 0.214686
1 0.119231
2 -1.665089
3 -0.099371
4 1.048900
Name: rename_test, dtype: float64
5 ------------------------------
rename_test test