亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何構造屬性在某個范圍內的 Python pandas 系列對象

如何構造屬性在某個范圍內的 Python pandas 系列對象

一只斗牛犬 2022-06-22 18:21:43
我有一個 pandas.Series 對象,其中每個對象 t 有幾個屬性,其中一個是它的長度 t.len。我想創建另一個系列 SL,其中包含 S 中長度在 S 中對象的第 60 個百分位和第 90 個百分位之間的那些對象。對此進行編碼的最有效方法是什么?假設S = [t0, t1, t2, t3, t4, t5, t6, t7, t8, t9]是一系列 10 個對象。它們對應的長度列表是[15, 4, 10, 20, 3, 20, 13, 8, 14, 1]。第 60 個百分位長度為 13.4,第 90 個百分位長度為 20。那么SL = [t0, t3, t5, t8]下面是基于series.between的代碼,但是會產生錯誤,即: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 回答

?
開滿天機

TA貢獻1786條經驗 獲得超13個贊

您可以使用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


查看完整回答
反對 回復 2022-06-22
  • 1 回答
  • 0 關注
  • 159 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號