Упрощенный пример
У меня есть CSV-файл, который я хочу просмотреть по строкам и посчитать, сколько раз значение появляется в определенном диапазоне. Мой CSV составляет около 500 строк, но вот упрощенный пример того, как это выглядит:
Time key
0 -5 LeftShift
1 10 LeftShift
2 20 C
3 30 LeftShift
4 40 C
Я пытаюсь подсчитать, сколько раз строка "C" встречается в определенном временном диапазоне. Я также хотел бы посчитать, сколько раз "C" происходит за пределами временного диапазона.
import pandas as pd
# Name of file to read
filename = "test.csv"
#data frame to create from file
df = pd.read_csv(filename)
# Set counters
correct_hit = 0
incorrect_hit = 0
# Time range (milliseconds) to compare
range1 = 0
range2 = 20
range3 = 20.01
range4 = 40
if (range1 < Time <= range2) and (Key == "C"):
incorrect_hit += 1
elif (range3 < Time <= range4) and (Key == "C"):
correct_hit += 1
У меня есть общая логика в конце моего примера кода. Я сравниваю диапазон времени с переменной «Time», которая еще не определена. Вот где я застрял.
Для примера, который я привел выше, я ожидаю, что мои результаты будут равны значениям счетчика:
incorrect_hit = 1
correct_hit = 1
Спасибо @PaSTE за ваш ответ и направление,
Другая попытка
import pandas as pd
# Name of file to read
filename = "TEST.csv"
#data frame to create from file
df = pd.read_csv(filename)
# Set counters
correct_hit = 0
incorrect_hit = 0
# Time ranges (milliseconds) for correct_hits
# range1-2 & 3-4 are correct hits
range1 = 92001
range2 = 96000
range3 = 376004
range4 = 380004
# Time ranges (milliseconds) for incorrect_hits
range5 = 12000
range6 = 16000
range7 = 28000
range8 = 32000
range9 = 44001
range10 = 48001
range11 = 60000
range12 = 64001
#range 13 - 14 was incorrect hit
range13 = 76000
range14 = 80001
#
range15 = 108001
range16 = 112001
range17 = 124001
range18 = 128001
range19 = 140001
range20 = 144001
range21 = 156002
range22 = 160001
range23 = 172002
range24 = 176002
range25 = 188002
range26 = 192002
range27 = 204002
range28 = 208002
range29 = 280003
range30 = 284003
range31 = 296003
range32 = 300003
range33 = 312003
range34 = 316003
range35 = 328004
range36 = 332003
range37 = 344004
range38 = 348004
range39 = 360004
range40 = 364004
# range 41 to 42 was incorrect hit
range41 = 392004
range42 = 396005
#
range43 = 408004
range44 = 412005
range45 = 424004
range46 = 428004
range47 = 440005
range48 = 444004
range49 = 456005
range50 = 460005
range51 = 472005
range52 = 476005
# This compares the number of correct responses
condition1 = (df["Time"] >= range1) & (df["Time"] <= range2) & (df["key"] == "C")
condition2 = (df["Time"] >= range3) & (df["Time"] <= range4) & (df["key"] == "C")
correct_hit = condition1.sum() + condition2.sum()
# This compares the number of incorrect responses
condition3 = (df["Time"] >= range5) & (df["Time"] <= range6) & (df["key"] == "C")
condition4 = (df["Time"] >= range7) & (df["Time"] <= range8) & (df["key"] == "C")
condition5 = (df["Time"] >= range9) & (df["Time"] <= range10) & (df["key"] == "C")
condition6 = (df["Time"] >= range11) & (df["Time"] <= range12) & (df["key"] == "C")
condition7 = (df["Time"] >= range13) & (df["Time"] <= range14) & (df["key"] == "C")
condition8 = (df["Time"] >= range15) & (df["Time"] <= range16) & (df["key"] == "C")
condition9 = (df["Time"] >= range17) & (df["Time"] <= range18) & (df["key"] == "C")
condition10 = (df["Time"] >= range19) & (df["Time"] <= range20) & (df["key"] == "C")
condition11 = (df["Time"] >= range21) & (df["Time"] <= range22) & (df["key"] == "C")
condition12 = (df["Time"] >= range23) & (df["Time"] <= range24) & (df["key"] == "C")
condition13 = (df["Time"] >= range25) & (df["Time"] <= range26) & (df["key"] == "C")
condition14 = (df["Time"] >= range27) & (df["Time"] <= range28) & (df["key"] == "C")
condition15 = (df["Time"] >= range29) & (df["Time"] <= range30) & (df["key"] == "C")
condition16 = (df["Time"] >= range31) & (df["Time"] <= range32) & (df["key"] == "C")
condition17 = (df["Time"] >= range33) & (df["Time"] <= range34) & (df["key"] == "C")
condition18 = (df["Time"] >= range35) & (df["Time"] <= range36) & (df["key"] == "C")
condition19 = (df["Time"] >= range37) & (df["Time"] <= range38) & (df["key"] == "C")
condition20 = (df["Time"] >= range39) & (df["Time"] <= range40) & (df["key"] == "C")
condition21 = (df["Time"] >= range41) & (df["Time"] <= range42) & (df["key"] == "C")
condition22 = (df["Time"] >= range43) & (df["Time"] <= range44) & (df["key"] == "C")
condition23 = (df["Time"] >= range45) & (df["Time"] <= range46) & (df["key"] == "C")
condition24 = (df["Time"] >= range47) & (df["Time"] <= range48) & (df["key"] == "C")
condition25 = (df["Time"] >= range49) & (df["Time"] <= range50) & (df["key"] == "C")
condition26 = (df["Time"] >= range51) & (df["Time"] <= range52) & (df["key"] == "C")
incorrect_hit = condition3.sum() + condition4.sum() + condition5.sum() + condition6.sum() + condition7.sum() + condition8.sum() + condition9.sum() + condition10.sum() + condition11.sum() + condition12.sum() + condition13.sum() + condition14.sum() + condition15.sum() + condition16.sum() + condition17.sum() + condition18.sum() + condition19.sum() + condition20.sum() + condition21.sum() + condition22.sum() + condition23.sum() + condition24.sum() + condition25.sum() + condition26.sum()
print(f'The number of correct hits: {correct_hit}')
print(f'The number of incorrect hits: {incorrect_hit}')
Ожидаемый результат
Количество правильных попаданий: 2 Количество неверных попаданий: 2
Что верно. Я также пытался получить другой набор, чтобы найти число случаев, когда «С» происходило за пределами временных диапазонов, но у меня ничего не вышло, поэтому я просто удалил его из фрагмента кода.
Еще раз спасибо @PaSTE. Я подозреваю, что есть лучшие способы сделать это, но я думаю, что пока это работает:)