I think that can be done not on the call of loc
, but yes when you get the variables from the user. First, we need to consider an entry that the user did not provide as None
. Currently, you are considering an empty string (''
). So we need to convert her:
def retorna_variavel(nome):
# Insira aqui o código para obter a entrada do usuário pela interface
# gráfica. Estou assumindo que a entrada esteja sendo obtida por meio
# de uma caixa de texto por exemplo, portanto ela será do tipo string
texto = retorna_variavel_de_caixa_de_texto(nome)
# Verifica se o usuário não digitou nada. Essa verificação é equivalente
# à `if texto is None or len(texto) == 0:`
if not texto:
return None
# Converte a variável inserida pelo usuário para um tipo inteiro. Você
# também pode convertê-la para o tipo ponto flutuante utilizando `float`.
return int(texto)
Then you create the variables a
, b
, c
and d
desired:
a = retorna_variavel('a')
b = retorna_variavel('b')
c = retorna_variavel('c')
d = retorna_variavel('d')
Finally, you can use the following code to filter the pd.DataFrame
:
import pandas as pd
# Um DataFrame de exemplo
chunk = pd.DataFrame({
'Coluna1': [1, 1],
'Coluna2': [2, 2],
'Coluna3': [3, 3],
'Coluna4': [5, 6],
})
arquivo = chunk.loc[
(a is None or chunk['Coluna1'] == a) &
(b is None or chunk['Coluna2'] == b) &
(c is None or chunk['Coluna3'] == c) &
(d is None or chunk['Coluna4'] == d)]
print(arquivo)
In this example,
- case
a == 1
, b == 2
, c == 3
and d == 4
, the exit will be
Empty DataFrame
Columns: [Coluna1, Coluna2, Coluna3, Coluna4]
Index: []
- case
a == 1
, b == 2
, c == 3
and d == 5
, the exit will be
Coluna1 Coluna2 Coluna3 Coluna4
0 1 2 3 5
- case
a == 1
, b == 2
, c == 3
and d == 6
, the exit will be
Coluna1 Coluna2 Coluna3 Coluna4
1 1 2 3 6
- case
a == 1
, b == 2
, c == 3
and d == None
(the user has omitted the value of d
), the exit will be
Coluna1 Coluna2 Coluna3 Coluna4
0 1 2 3 5
1 1 2 3 6
Like d
was omitted, all values in column 4 were returned.
These entries are given by a graphical user interface. I necessarily receive values for all 4, but those that are not filled in, I receive: "". In my logic, I needed the Loc function to ignore the unfilled variables, but I’m not able to think of a way to do that... The wildcard variable I meant would be a value by which the Loc function would search in the respective column, but to bring something, you know?
– Gabriel Ocker
I understand, I apologize for the misunderstanding. I updated my answer, see if it solves your problem.
– enzo