Assign a filtered value with pandas to a python variable

Question:

I’m using pandas to filter results from a column in the worksheet, and I need to assign some values to variables, but I can’t

My code snippet below makes the filter

variable = clientes.loc[(clientes['Data']=='08/02/2023')]
print(variable)

Below the result

 Nome do paciente        Data Horário  Email     Telefone Tipo de agendamento           Situação Status Financeiro Observação
34          Teste 1  08/02/2023   10h00    NaN          NaN                 NaN  Paciente agendado          Não pago        NaN
35          Teste 2  08/02/2023   13h30    NaN          NaN                 NaN  Paciente agendado          Não pago        NaN
36          Teste 3  08/02/2023   15h00    NaN   55544454.0    Consulta Simples  Paciente agendado          Não pago        NaN
37          Teste 4  08/02/2023   18h00    NaN  555445454.0    Consulta Simples  Paciente agendado          Não pago        NaN
38          Teste 5  08/02/2023   20h00    NaN          NaN                 NaN  Paciente agendado          Não pago        NaN

Below are the values I need to assign to a variable. ideally a list

   Nome do paciente      Data      Horário
    Teste 1            08/02/2023   10h00
    Teste 2            08/02/2024   13h30
    Teste 3            08/02/2025   15h00
    Teste 4            08/02/2026   18h00
    Teste 5            08/02/2027   20h00

I checked the solution from the link below, but I couldn’t adapt it to my code

Pandas assigning value to variable with two column filters from dataframe

Asked By: Brandalize

||

Answers:

You are not specifying the columns which you need. Try the below code.

variable = clientes.loc[(clientes['Data']=='08/02/2023')][['Nome do paciente',       'Data', 'Horário']]

print(variable)

final_list = []

[final_list.extend(i) for in variable.values.tolist()]

print(final_list)

final_list will give you the list like below

[‘Teste 1′, ’08/02/2023′, ’10h00’, ‘Teste 2′, ’08/02/2024’, ’13h30’……………….]

Answered By: Manoj biroj
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.