【问题标题】:Get a specific value from a result set in Python using where condition使用 where 条件从 Python 中的结果集中获取特定值
【发布时间】:2022-12-19 22:49:32
【问题描述】:

我正在使用 pandas 从 excel 文件中读取一组值。在这些值中,我需要一个特定的值单独使用 where 条件打印。

你能帮忙写下python脚本吗?

示例:- BusinessProcess = Process1 和 Configs = EmailRecipients 的值是多少 预期答案 - ABC@gmail.com(没有任何其他索引号或数据类型)。我需要将其分配给一个变量并将其用作其他一些功能的配置。

此外,我只是不想要基于索引号的 where 条件,如 [0]。它应该纯粹使用其他列数据过滤数据。

enter image description here

在下图中,我得到了带有索引号、名称和数据类型的结果。但我只需要 ABC@gmail.com 的结果,因为我需要将它分配给另一个函数的另一个变量。

enter image description here

【问题讨论】:

  • 尝试使用.values

标签: python pandas dataframe


【解决方案1】:

应用布尔索引后,您将获得满足条件的所有“值”列的行。换句话说,您将获得过滤后的列,它是一个具有某些属性的 pandas.Series 对象,包括您不需要的索引。 为了从 Series 访问/提取值,可以使用 padnas Series 对象的 to_list() 方法:

df = pd.DataFrame(data={'values':['v1','v2','v3'], 'property1':[1,2,3], 
'property2':[3,4,5]})
values_lst = df[(df['property1'] == 2) & (df['property2'] ==4)]['values'].to_list()
values_lst[0]

>>'v2'

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-23
    • 2014-08-31
    • 2021-09-25
    相关资源
    最近更新 更多