Pandas數(shù)據(jù)結構Series:基本概念及創(chuàng)建
"一維數(shù)組"Serise
`
Series 數(shù)據(jù)結構
Series 是帶有標簽的一維數(shù)組,可以保存任何數(shù)據(jù)類型(整數(shù)使鹅,字符串权她,浮點數(shù)晤揣,Python對象等),軸標簽統(tǒng)稱為索引
導入numpy旗芬、pandas模塊
import numpy as np
import pandas as pd
s = pd.Series(np.random.rand(6))
查看數(shù)據(jù)、數(shù)據(jù)類型
print(1,'-'30)
print(s)
print(2,'-'30)
print(type(s))
.index查看series索引升敲,類型為rangeindex
.values查看series值答倡,類型是ndarray
核心:series相比于ndarray,是一個自帶索引index的數(shù)組 → 一維數(shù)組 + 對應索引
所以當只看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 創(chuàng)建方法一:由字典創(chuàng)建,字典的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}
#執(zhí)行結果
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 創(chuàng)建方法二:由數(shù)組創(chuàng)建(一維數(shù)組)
arr = np.random.randn(4)
s = pd.Series(arr)
print(1,'-'*30)
print(arr)
print(2,'-'*30)
print(s)
# 默認index是從0開始度帮,步長為1的數(shù)字
s = pd.Series(arr, index = ['a','b','c','d'],dtype = np.object)
print(3,'-'*30)
print(s)
# index參數(shù):設置index歼捏,長度保持一致
# dtype參數(shù):設置數(shù)值類型
#運行結果
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 創(chuàng)建方法三:由標量創(chuàng)建
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的一個參數(shù),創(chuàng)建一個數(shù)組的 名稱
# .name方法:輸出數(shù)組的名稱率翅,輸出格式為str练俐,如果沒用定義輸出名稱,輸出為None
s3 = s2.rename('rename_test')
print(4,'-'*30)
print(s3)
print(5,'-'*30)
print(s3.name, s2.name)
# .rename()重命名一個數(shù)組的名稱冕臭,并且新指向一個數(shù)組腺晾,原數(shù)組不變
#執(zhí)行結果
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