# Series 數(shù)據(jù)結(jié)構(gòu)
# Series 是帶有標簽的一維數(shù)組,可以保存任何數(shù)據(jù)類型(整數(shù),字符串,浮點數(shù),Python對象等),軸標簽統(tǒng)稱為索引
import numpy as np
import pandas as pd
s = pd.Series(np.random.rand(5))
print(s)
print(s.index,type(s.index))
print(s.values,type(s.values))
[output]:
RangeIndex(start=0, stop=5, step=1) <class 'pandas.core.indexes.range.RangeIndex'>
[0.41310443 0.51893698 0.5697928 0.10437544 0.64705221] <class 'numpy.ndarray'>
#創(chuàng)建方法:
# Series 創(chuàng)建方法一:由字典創(chuàng)建,字典的key就是index,values就是values
dic = {'a':1 ,'b':2 , 'c':3, '4':4, '5':5}
s = pd.Series(dic)
# Series 創(chuàng)建方法二:由數(shù)組創(chuàng)建(一維數(shù)組)
arr = np.random.randn(5)
s = pd.Series(arr)
print(arr)
print(s)
# 默認index是從0開始,步長為1的數(shù)字
s = pd.Series(arr, index = ['a','b','c','d','e'],dtype = np.object)
print(s)
# index參數(shù):設(shè)置index,長度保持一致
# dtype參數(shù):設(shè)置數(shù)值類型
# Series 創(chuàng)建方法三:由標量創(chuàng)建
s = pd.Series(10, index = range(4))
print(s)
# 如果data是標量值,則必須提供索引。該值會重復(fù),來匹配索引的長度
# Series 名稱屬性:name
s1 = pd.Series(np.random.randn(5))
print(s1)
s2 = pd.Series(np.random.randn(5),name = 'test')
print(s2)
s3 = s2.rename('hehehe')
print(s3)
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。