我有一個 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
添加回答
舉報
0/150
提交
取消