99999久久久久久亚洲,欧美人与禽猛交狂配,高清日韩av在线影院,一个人在线高清免费观看,啦啦啦在线视频免费观看www

熱線電話:13121318867

登錄
首頁(yè)大數(shù)據(jù)時(shí)代pandas數(shù)據(jù)結(jié)構(gòu):Series
pandas數(shù)據(jù)結(jié)構(gòu):Series
2020-06-16
收藏

pandasSeriesDataFrame兩種數(shù)據(jù)結(jié)構(gòu),我們之前已經(jīng)講過(guò)了DataFrame,接下來(lái)給大家介紹下另一種數(shù)據(jù)結(jié)構(gòu)Series

什么是Series?

# 自定義Series索引

arr = np.random.rand(5)
s = pd.Series(arr, index=list("abcde"))
print(s)
a    0.239432
b    0.554542
c    0.058231
d    0.211549
e    0.362285
dtype: float64
[ 0.67962276  0.76999562  0.95308305  0.66162424  0.93883112]

0    0.679623
1    0.769996
2    0.953083
3    0.661624
4    0.938831
dtype: float64

RangeIndex(start=0, stop=5, step=1) <class 'pandas.core.indexes.range.RangeIndex'>

[0, 1, 2, 3, 4]

[ 0.67962276  0.76999562  0.95308305  0.66162424  0.93883112]
# 自定義Series索引

arr = np.random.rand(5)
s = pd.Series(arr, index=list("abcde"))
print(s)
a    0.239432
b    0.554542
c    0.058231
d    0.211549
e    0.362285
dtype: float64

Series創(chuàng)建方法

# 通過(guò)標(biāo)量創(chuàng)建

s = pd.Series(100, index=range(5))
print(s)
0    100
1    100
2    100
3    100
4    100
dtype: int64
# 通過(guò)標(biāo)量創(chuàng)建

s = pd.Series(100, index=range(5))
print(s)
0    100
1    100
2    100
3    100
4    100
dtype: int64
# 通過(guò)標(biāo)量創(chuàng)建

s = pd.Series(100, index=range(5))
print(s)
0    100
1    100
2    100
3    100
4    100
dtype: int64

Series下標(biāo)索引

arr = np.random.rand(5)*100
s = pd.Series(arr, index=[chr(i) for i in range(97, 97+len(arr))])
print(s)
print("")

bool_index = s>50  # 布爾型索引
print(bool_index)
print("")

print(s[s>50])  # 用bool_index取出s中大于50的值
a    24.447599
b     0.795073
c    49.464825
d     9.987239
e    86.314340
dtype: float64

a    False
b    False
c    False
d    False
e     True
dtype: bool

e    86.31434
dtype: float64
a    0.001694
b    0.107466
c    0.272233
d    0.637616
e    0.875348
dtype: float64

0.107465887721

0.107465887721

b    0.107466
d    0.637616
dtype: float64

a    0.001694
c    0.272233
dtype: float64

Series切片

print(s)
s["f"] = None  # 給s添加一個(gè)空值
s["g"] = np.nan  # np.nan 代表有問(wèn)題的值 也會(huì)識(shí)別為空值
print("")

print(s)
print("")

bool_index1 = s.isnull()  # 判斷那些值是空值: 空值是True 非空為False
print(bool_index1)
print("")

print(s[bool_index1])  # 取出空值
print("")

bool_index2 = s.notnull()  # 判斷那些值是非空值: 空值是False 非空為T(mén)rue
print(bool_index2)
print("")

print(s[bool_index2])  # 取出非空值
a     24.4476
b    0.795073
c     49.4648
d     9.98724
e     86.3143
f        None
g         NaN
dtype: object

a     24.4476
b    0.795073
c     49.4648
d     9.98724
e     86.3143
f        None
g         NaN
dtype: object

a    False
b    False
c    False
d    False
e    False
f     True
g     True
dtype: bool

f    None
g     NaN
dtype: object

a     True
b     True
c     True
d     True
e     True
f    False
g    False
dtype: bool

a     24.4476
b    0.795073
c     49.4648
d     9.98724
e     86.3143
dtype: object

Series布爾型索引

print(s)
s["f"] = None  # 給s添加一個(gè)空值
s["g"] = np.nan  # np.nan 代表有問(wèn)題的值 也會(huì)識(shí)別為空值
print("")

print(s)
print("")

bool_index1 = s.isnull()  # 判斷那些值是空值: 空值是True 非空為False
print(bool_index1)
print("")

print(s[bool_index1])  # 取出空值
print("")

bool_index2 = s.notnull()  # 判斷那些值是非空值: 空值是False 非空為T(mén)rue
print(bool_index2)
print("")

print(s[bool_index2])  # 取出非空值
a     24.4476
b    0.795073
c     49.4648
d     9.98724
e     86.3143
f        None
g         NaN
dtype: object

a     24.4476
b    0.795073
c     49.4648
d     9.98724
e     86.3143
f        None
g         NaN
dtype: object

a    False
b    False
c    False
d    False
e    False
f     True
g     True
dtype: bool

f    None
g     NaN
dtype: object

a     True
b     True
c     True
d     True
e     True
f    False
g    False
dtype: bool

a     24.4476
b    0.795073
c     49.4648
d     9.98724
e     86.3143
dtype: object
print(s)
s["f"] = None  # 給s添加一個(gè)空值
s["g"] = np.nan  # np.nan 代表有問(wèn)題的值 也會(huì)識(shí)別為空值
print("")

print(s)
print("")

bool_index1 = s.isnull()  # 判斷那些值是空值: 空值是True 非空為False
print(bool_index1)
print("")

print(s[bool_index1])  # 取出空值
print("")

bool_index2 = s.notnull()  # 判斷那些值是非空值: 空值是False 非空為T(mén)rue
print(bool_index2)
print("")

print(s[bool_index2])  # 取出非空值
a     24.4476
b    0.795073
c     49.4648
d     9.98724
e     86.3143
f        None
g         NaN
dtype: object

a     24.4476
b    0.795073
c     49.4648
d     9.98724
e     86.3143
f        None
g         NaN
dtype: object

a    False
b    False
c    False
d    False
e    False
f     True
g     True
dtype: bool

f    None
g     NaN
dtype: object

a     True
b     True
c     True
d     True
e     True
f    False
g    False
dtype: bool

a     24.4476
b    0.795073
c     49.4648
d     9.98724
e     86.3143
dtype: object

Series基本技巧

查看數(shù)據(jù)

import numpy as np
import pandas as pd
s = pd.Series(np.random.rand(15))
print(s)
print("")

print(s.head())  # 查看前5條數(shù)據(jù)
print("")

print(s.head(2))  # 查看前2條數(shù)據(jù)
print("")

print(s.tail())  # 查看后5條數(shù)據(jù)
print("")

print(s.tail(2))  # 查看后兩條數(shù)據(jù)
0     0.049732
1     0.281123
2     0.398361
3     0.492084
4     0.555350
5     0.729037
6     0.603854
7     0.643413
8     0.951804
9     0.459948
10    0.261974
11    0.897656
12    0.428898
13    0.426533
14    0.301044
dtype: float64

0    0.049732
1    0.281123
2    0.398361
3    0.492084
4    0.555350
dtype: float64

0    0.049732
1    0.281123
dtype: float64

10    0.261974
11    0.897656
12    0.428898
13    0.426533
14    0.301044
dtype: float64

13    0.426533
14    0.301044
dtype: float64

重置索引

# reindex 與給索引重新命名不同

s = pd.Series(np.random.rand(5), index=list("bdeac"))
print(s)
print("")

s1 = s.reindex(list("abcdef"))  # Series的reindex使它符合新的索引,如果索引不存在就自動(dòng)填入空值
print(s1)
print("")

print(s)  # 不會(huì)改變?cè)瓟?shù)組
print("")

s2 = s.reindex(list("abcdef"), fill_value=0)  # 如果索引值不存在就自定義填入缺失值
print(s2)
b    0.539124
d    0.853346
e    0.065577
a    0.406689
c    0.562758
dtype: float64

a    0.406689
b    0.539124
c    0.562758
d    0.853346
e    0.065577
f         NaN
dtype: float64

b    0.539124
d    0.853346
e    0.065577
a    0.406689
c    0.562758
dtype: float64

a    0.406689
b    0.539124
c    0.562758
d    0.853346
e    0.065577
f    0.000000
dtype: float64

對(duì)齊

s1 = pd.Series(np.random.rand(3), index=list("abc"))
s2 = pd.Series(np.random.rand(3), index=list("cbd"))
print(s1)
print("")

print(s2)
print("")

print(s1+s2)  # 對(duì)應(yīng)的標(biāo)簽相加  缺失值加任何值還是缺失值
a    0.514657
b    0.618971
c    0.456840
dtype: float64

c    0.083065
b    0.893543
d    0.125063
dtype: float64

a         NaN
b    1.512513
c    0.539905
d         NaN
dtype: float64

刪除

# Series.drop("索引名") 

s = pd.Series(np.random.rand(5), index=list("abcde"))
print(s)
print("")

s1 = s.drop("b")  # 一次刪除一個(gè)并返回副本
print(s1)
print("")

s2 = s.drop(["d", "e"])  # 一次刪除兩個(gè)并返回副本
print(s2)
print("")

print(s)  # 驗(yàn)證原數(shù)沒(méi)有改變
a    0.149823
b    0.330215
c    0.069852
d    0.967414
e    0.867417
dtype: float64

a    0.149823
c    0.069852
d    0.967414
e    0.867417
dtype: float64

a    0.149823
b    0.330215
c    0.069852
dtype: float64

a    0.149823
b    0.330215
c    0.069852
d    0.967414
e    0.867417
dtype: float64
s = pd.Series(np.random.rand(5), index=list("abcde"))
print(s)
print("")

s1 = s.drop(["b", "c"], inplace=True)  # inplace默認(rèn)是False 改為T(mén)rue后不會(huì)返回副本 直接修改原數(shù)組
print(s1)
print("")

print(s)  # 驗(yàn)證原數(shù)組已改變
a    0.753187
b    0.077156
c    0.626230
d    0.428064
e    0.809005
dtype: float64

None

a    0.753187
d    0.428064
e    0.809005
dtype: float64

添加

s1 = pd.Series(np.random.rand(5), index=list("abcde"))
print(s1)
print("")

# 通過(guò)索引標(biāo)簽添加
s1["f"] = 100
print(s1)
print("")

# 通過(guò)append添加一個(gè)數(shù)組 并返回一個(gè)新的數(shù)組

s2 = s1.append(pd.Series(np.random.rand(2), index=list("mn")))
print(s2)
a    0.860190
b    0.351980
c    0.237463
d    0.159595
e    0.119875
dtype: float64

a      0.860190
b      0.351980
c      0.237463
d      0.159595
e      0.119875
f    100.000000
dtype: float64

a      0.860190
b      0.351980
c      0.237463
d      0.159595
e      0.119875
f    100.000000
m      0.983410
n      0.293722
dtype: float64

數(shù)據(jù)分析咨詢(xún)請(qǐng)掃描二維碼

若不方便掃碼,搜微信號(hào):CDAshujufenxi

數(shù)據(jù)分析師資訊
更多

OK
客服在線
立即咨詢(xún)
客服在線
立即咨詢(xún)
') } function initGt() { var handler = function (captchaObj) { captchaObj.appendTo('#captcha'); captchaObj.onReady(function () { $("#wait").hide(); }).onSuccess(function(){ $('.getcheckcode').removeClass('dis'); $('.getcheckcode').trigger('click'); }); window.captchaObj = captchaObj; }; $('#captcha').show(); $.ajax({ url: "/login/gtstart?t=" + (new Date()).getTime(), // 加隨機(jī)數(shù)防止緩存 type: "get", dataType: "json", success: function (data) { $('#text').hide(); $('#wait').show(); // 調(diào)用 initGeetest 進(jìn)行初始化 // 參數(shù)1:配置參數(shù) // 參數(shù)2:回調(diào),回調(diào)的第一個(gè)參數(shù)驗(yàn)證碼對(duì)象,之后可以使用它調(diào)用相應(yīng)的接口 initGeetest({ // 以下 4 個(gè)配置參數(shù)為必須,不能缺少 gt: data.gt, challenge: data.challenge, offline: !data.success, // 表示用戶(hù)后臺(tái)檢測(cè)極驗(yàn)服務(wù)器是否宕機(jī) new_captcha: data.new_captcha, // 用于宕機(jī)時(shí)表示是新驗(yàn)證碼的宕機(jī) product: "float", // 產(chǎn)品形式,包括:float,popup width: "280px", https: true // 更多配置參數(shù)說(shuō)明請(qǐng)參見(jiàn):http://docs.geetest.com/install/client/web-front/ }, handler); } }); } function codeCutdown() { if(_wait == 0){ //倒計(jì)時(shí)完成 $(".getcheckcode").removeClass('dis').html("重新獲取"); }else{ $(".getcheckcode").addClass('dis').html("重新獲取("+_wait+"s)"); _wait--; setTimeout(function () { codeCutdown(); },1000); } } function inputValidate(ele,telInput) { var oInput = ele; var inputVal = oInput.val(); var oType = ele.attr('data-type'); var oEtag = $('#etag').val(); var oErr = oInput.closest('.form_box').next('.err_txt'); var empTxt = '請(qǐng)輸入'+oInput.attr('placeholder')+'!'; var errTxt = '請(qǐng)輸入正確的'+oInput.attr('placeholder')+'!'; var pattern; if(inputVal==""){ if(!telInput){ errFun(oErr,empTxt); } return false; }else { switch (oType){ case 'login_mobile': pattern = /^1[3456789]\d{9}$/; if(inputVal.length==11) { $.ajax({ url: '/login/checkmobile', type: "post", dataType: "json", data: { mobile: inputVal, etag: oEtag, page_ur: window.location.href, page_referer: document.referrer }, success: function (data) { } }); } break; case 'login_yzm': pattern = /^\d{6}$/; break; } if(oType=='login_mobile'){ } if(!!validateFun(pattern,inputVal)){ errFun(oErr,'') if(telInput){ $('.getcheckcode').removeClass('dis'); } }else { if(!telInput) { errFun(oErr, errTxt); }else { $('.getcheckcode').addClass('dis'); } return false; } } return true; } function errFun(obj,msg) { obj.html(msg); if(msg==''){ $('.login_submit').removeClass('dis'); }else { $('.login_submit').addClass('dis'); } } function validateFun(pat,val) { return pat.test(val); }