第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

如何構(gòu)造屬性在某個(gè)范圍內(nèi)的 Python pandas 系列對象

如何構(gòu)造屬性在某個(gè)范圍內(nèi)的 Python pandas 系列對象

一只斗牛犬 2022-06-22 18:21:43
我有一個(gè) pandas.Series 對象,其中每個(gè)對象 t 有幾個(gè)屬性,其中一個(gè)是它的長度 t.len。我想創(chuàng)建另一個(gè)系列 SL,其中包含 S 中長度在 S 中對象的第 60 個(gè)百分位和第 90 個(gè)百分位之間的那些對象。對此進(jìn)行編碼的最有效方法是什么?假設(shè)S = [t0, t1, t2, t3, t4, t5, t6, t7, t8, t9]是一系列 10 個(gè)對象。它們對應(yīng)的長度列表是[15, 4, 10, 20, 3, 20, 13, 8, 14, 1]。第 60 個(gè)百分位長度為 13.4,第 90 個(gè)百分位長度為 20。那么SL = [t0, t3, t5, t8]下面是基于series.between的代碼,但是會(huì)產(chǎn)生錯(cuò)誤,即:TypeError: list indices must be integers or slices, not Seriesimport numpy as npimport pandas as pdclass Object:    def __init__(self, tid, length):        self.tid = tid                self.len = lengthobjectseries = pd.Series([Object(0, 15), Object(1, 4), Object(2, 10), Object(3, 20), Object(4, 3), Object(5, 20), Object(6, 13), Object(7, 8), Object(8, 14), Object(9, 1)])lenseries = pd.Series(x.len for x in objectseries)ll = np.percentile(lenseries, 60)uu = np.percentile(lenseries, 90)sl = lenseries.between(ll,uu)print (sl)objectlist = objectseries.tolist()print (objectlist[sl])
查看完整描述

1 回答

?
開滿天機(jī)

TA貢獻(xiàn)1786條經(jīng)驗(yàn) 獲得超13個(gè)贊

您可以使用quantile獲取百分位值并使用between:


df = pd.DataFrame({'object':[f't{i}' for i in range(10)],

              'values':[15, 4, 10, 20, 3, 20, 13, 8, 14, 1]})


q60,q90 = df['values'].quantile([0.6, 0.9])


df.loc[df['values'].between(q60,q90), 'object']

輸出:


0    t0

3    t3

5    t5

8    t8

Name: object, dtype: object您可以使用quantile獲取百分位值并使用between:


df = pd.DataFrame({'object':[f't{i}' for i in range(10)],

              'values':[15, 4, 10, 20, 3, 20, 13, 8, 14, 1]})


q60,q90 = df['values'].quantile([0.6, 0.9])


df.loc[df['values'].between(q60,q90), 'object']

輸出:


0    t0

3    t3

5    t5

8    t8

Name: object, dtype: object


查看完整回答
反對 回復(fù) 2022-06-22
  • 1 回答
  • 0 關(guān)注
  • 160 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號