簡體   English   中英

統計小於某個值的id

[英]count id which are smaller than certain value

我的數據由與某個點有一定距離的唯一 ID 組成。 目標是計算等於或小於半徑的 id。

以下示例顯示了我的 DataFrame:

id    distance  radius
111   0.5       1
111   2         1
111   1         1
222   1         2
222   3         2
333   5         3
333   4         3

output 應如下所示:

id  count
111 2
222 1
333 0

你可以做:

df['distance'].le(df['radius']).groupby(df['id']).sum()

Output:

id
111    2.0
222    1.0
333    0.0
dtype: float64

或者你可以這樣做:

(df.loc[df.distance <= df.radius, 'id']
   .value_counts()
   .reindex(df['id'].unique(), fill_value=0)
)

Output:

111    2
222    1
333    0
Name: id, dtype: int64

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM