使用Pandas将一个经过筛选的数值分配给一个Python变量。

huangapple go评论92阅读模式
英文:

Assign a filtered value with pandas to a python variable

问题

以下是翻译好的部分:

  1. 我正在使用pandas来从工作表中筛选列中的结果并需要将一些值分配给变量但我不能
  2. 我的代码片段如下进行筛选
  3. variable = clientes.loc[(clientes['Data']=='08/02/2023')]
  4. print(variable)

以下是需要分配给变量的值,最好是一个列表

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

我查看了以下链接的解决方案,但无法将其适应到我的代码

链接地址

英文:

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

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

Below the result

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

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

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

https://stackoverflow.com/questions/61745393/pandas-assigning-value-to-variable-with-two-column-filters-from-dataframe

答案1

得分: 1

你没有指定所需的列。尝试以下代码。

  1. variable = clientes.loc[(clientes['Data']=='08/02/2023')][['Nome do paciente', 'Data', 'Horário']]
  2. print(variable)
  3. final_list = []
  4. [final_list.extend(i) for i in variable.values.tolist()]
  5. print(final_list)

final_list 将会给你以下类似的列表:

  1. ['Teste 1', '08/02/2023', '10h00', 'Teste 2', '08/02/2024', '13h30', ...]
英文:

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

  1. variable = clientes.loc[(clientes['Data']=='08/02/2023')][['Nome do paciente', 'Data', 'Horário']]
  2. print(variable)
  3. final_list = []
  4. [final_list.extend(i) for in variable.values.tolist()]
  5. print(final_list)

final_list will give you the list like below

['Teste 1', '08/02/2023', '10h00', 'Teste 2', '08/02/2024', '13h30'...................]

huangapple
  • 本文由 发表于 2023年2月8日 20:39:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/75385919.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定