pandas--Serise基础操作

一维数组Serise

创建方式:

1 由字典创建,字典的key就是index,values就是values

import numpy as np
import pandas as pd  

dic = {'a':1 ,'b':2 , 'c':3, 'd':4, 'e':5}
s = pd.Series(dic)
print(s)

 

2 由数组创建(一维数组)

import numpy as np
import pandas as pd  

arr = np.random.randn(5)
s = pd.Series(arr)
#print(arr)
print('===========================”')
print(s)
# 默认index是从0开始,步长为1的数字
print('===========================”')
s = pd.Series(arr, index = ['a','b','c','d','e'],dtype = np.object)
print(s)
# index参数:设置index,长度保持一致
# dtype参数:设置数值类型

练习:分别由字典、数组的方式,创建以下要求的Series

1 字典方式:

import numpy as np
import pandas as pd  

dic = {'math':79,'eng':89,'computer':88,'literal':90}
print(pd.Series(dic))

 

2 数组方式:

import numpy as np
import pandas as pd  

import random
ar1 = random.sample(range(80,100),4)
s = pd.Series(ar1,index=['math','eng','computer','literal'])
s

注意:ar1 = random.sample(range(80,100),4),是一个生成数据很好用的方法

猜你喜欢

转载自blog.csdn.net/qq_14822691/article/details/83781139