關注 + 星標 ~ 有趣的不像個技術號
每晚九點,我們準時相約
大家好,我是黃同學
1、Series和ndarray都可以通過索引和切片訪問元素,切片返回的是原來的視圖,索引返回的是原來的拷貝。
視圖:相當於就是原來的一個快捷方式,底層共用同一份元素,不管修改哪一個數組中的值,另外一個數組中的值也相當於變化了。
拷貝:相當於將原來的東西複製了一份,因此內存中又會另外開闢一塊兒空間,用於存放複製後的這個新Series或新ndarray。
1)對於ndarray來說
① 切片
x = np.arange(1,13)
display(x)
y = x[1:6]
display(y)
display(y[2])
y[2] = 888
display(y)
display(x)
結果如下:
② 索引
x = np.arange(1,13)
display(x)
y = x[x>6] # 這裏傳入的是一個布爾索引
display(y)
display(y[2])
y[2] = 888
display(y)
display(x)
結果如下:
2)對於Series來說
np.random.randint(3,10,5)產生的是隨機數,每次運行的結果都是不一樣的。
① 切片
x = pd.Series(np.random.randint(3,10,5),index=list("abcde"))
display(x)
y = x[2:]
display(y)
display(y[1])
y[1] = 666
display(y)
display(x)
結果如下:
② 索引
x = pd.Series(np.random.randint(3,10,5),index=list("abcde"))
display(x)
y = x[x>5]
display(y)
display(y[1])
y[1] = 666
display(y)
display(x)
結果如下:
2、Series和ndarray中常用屬性比較
1)共同屬性:圖中前7個屬性,在Series和ndarray中具有相同的含義;
① 一維數組
② Series
2)Series特有屬性:index、values、name;
① index和value屬性
② name屬性
注意:name屬性包含兩方面的內容,一個指的是Series的名稱,一個指的是Series中索引的名稱。
近期文章,點擊圖片即可查看
後臺回覆關鍵詞「進羣」,即刻加入讀者交流羣~
五
numpy蹲,numpy蹲,numpy蹲完,pandas蹲
朱小五