Как найти количество значений в определенном диапазоне в пандах? - PullRequest
0 голосов
/ 11 февраля 2019

У меня есть pandas dataframe, который содержит список значений ошибок.Я хочу найти долю моих ошибок в определенных диапазонах, например, какой процент моих ошибок находится в пределах + -1%, + -5%, + -10%, + -20% и + -50% и т. Д. Гистограмма моегоданные показаны ниже:

enter image description here

До сих пор я рассматривал такие функции, как pd.cut () и plt.hist (), но библиотеки не кажутсячтобы дать мне ответ, где мои диапазоны перекрывают друг друга, поэтому мне приходится прибегать к очень длинной пользовательской функции, которая ниже:

def error_distribution(df):

  total_length = len(df.index)
  one_perc = five_perc = ten_perc = fifteen_perc = twenty_perc = thirty_perc \
    = fourty_perc = fifty_perc = over_fifty = 0

  for index, row in df.iterrows():
    value = abs(row['Errors'])

    if value <= 0.01:
      one_perc += 1
      five_perc += 1
      ten_perc += 1
      fifteen_perc += 1
      twenty_perc += 1
      thirty_perc += 1
      fourty_perc += 1
      fifty_perc += 1
    elif value <= 0.05:
      five_perc += 1
      ten_perc += 1
      fifteen_perc += 1
      twenty_perc += 1
      thirty_perc += 1
      fourty_perc += 1
      fifty_perc += 1      
    elif value <= 0.1:
      ten_perc += 1
      fifteen_perc += 1
      twenty_perc += 1
      thirty_perc += 1
      fourty_perc += 1
      fifty_perc += 1
    elif value <= 0.15:
      fifteen_perc += 1
      twenty_perc += 1
      thirty_perc += 1
      fourty_perc += 1
      fifty_perc += 1
    elif value <= 0.2:
      twenty_perc += 1
      thirty_perc += 1
      fourty_perc += 1
      fifty_perc += 1
    elif value <= 0.3:
      thirty_perc += 1
      fourty_perc += 1
      fifty_perc += 1
    elif value <= 0.4:
      fourty_perc += 1
      fifty_perc += 1
    elif value <= 0.5:
      fifty_perc += 1
    else:
      over_fifty += 1

  print("Sub  1%: {0:.2f}%".format(one_perc/total_length*100))
  print("Sub  5%: {0:.2f}%".format(five_perc/total_length*100))
  print("Sub 10%: {0:.2f}%".format(ten_perc/total_length*100))
  print("Sub 15%: {0:.2f}%".format(fifteen_perc/total_length*100))
  print("Sub 20%: {0:.2f}%".format(twenty_perc/total_length*100))
  print("Sub 30%: {0:.2f}%".format(thirty_perc/total_length*100))
  print("Sub 40%: {0:.2f}%".format(fourty_perc/total_length*100))
  print("Sub 50%: {0:.2f}%".format(fifty_perc/total_length*100))
  print("Over 50%: {0:.2f}%".format(over_fifty/total_length*100))

И вывод, который я ищу, таков:

error_distribution(error_dataset1)

Вывод:

Sub  1%: 16.55%
Sub  5%: 56.61%
Sub 10%: 71.62%
Sub 15%: 78.53%
Sub 20%: 82.97%
Sub 30%: 88.46%
Sub 40%: 91.09%
Sub 50%: 92.59%
Over 50%: 7.41%

Кто-нибудь знает стандартную библиотеку, которая могла бы сделать это?

1 Ответ

0 голосов
/ 11 февраля 2019

Можете ли вы попробовать следующее:

import numpy as np
arr = np.random.uniform(low=0, high=100, size=(200,))
count, division = np.histogram(arr, bins=[0, .01, 0.05, 0.1, 0.15, 0.2, 0.3, 0.4, 0.5, 1])
print(count, division)
...