This repository has been archived by the owner on Apr 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
utils.py
120 lines (86 loc) · 3.29 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
"""
MIT License
Copyright (c) 2022 UnB
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""
from typing import Any
from pandas import DataFrame
def filter_columns(df: DataFrame, *filters: Any) -> DataFrame:
"""
Esta função filtra as colunas do dataframe através dos parâmetros
`filters`.
"""
rows = df.values.tolist() # type: ignore
columns: list[str] = list(df.columns)
indexes: list[int] = []
new_df: list[list[int]] = []
for value in filters:
indexes.append(columns.index(value))
for row in rows:
values: list[int] = []
for index in indexes:
values.append(row[index])
new_df.append(values)
return DataFrame(new_df, columns=filters) # type: ignore
def filter_values(df: DataFrame, column: str, *values: Any) -> DataFrame:
"""
Esta função filtra as linhas do dataframe de acordo com os valores
de uma coluna.
"""
rows = df.values.tolist() # type: ignore
columns: list[str] = list(df.columns)
index = columns.index(column)
new_df: list[list[int]] = []
for row in rows:
if row[index] in values:
new_df.append(row)
return DataFrame(new_df, columns=columns)
def filter_range(df: DataFrame, column: str, values: list[int]) -> DataFrame:
"""
Esta função filtra as linhas do dataframe de acordo com um
intervalo.
"""
min = values[0]
max = values[1]
rows = df.values.tolist() # type: ignore
columns: list[str] = list(df.columns)
index = columns.index(column)
new_df: list[list[int]] = []
for row in rows:
if min <= row[index] <= max:
new_df.append(row)
return DataFrame(new_df, columns=columns)
def closest_value(values: list[int], number: int) -> int:
"""
Esta função retorna o valor mais próximo de um número.
"""
aux: list[int] = []
for value in values:
aux.append(abs(number - value))
return values[aux.index(min(aux))]
def get_all_values(df: DataFrame, column: str) -> list[Any]:
"""
Esta função retorna os valores de uma coluna específica
em forma de lista.
"""
rows = df.values.tolist() # type: ignore
columns: list[str] = list(df.columns)
index = columns.index(column)
values: set[Any] = set()
for row in rows:
values.add(row[index])
return list(values)