首页 > 解决方案 > 将特定单元格定义为 pandas 数据框中的变量

问题描述

嗨我有以下数据框

Name    Age    Height    Country
Jack    18      185       AU
Jill    16      159       NZ
Geoff   16      177       US
Tess    15      155       AU

我试图找到一个特定的行,然后定义两个变量以用于其他计算。

例如我想知道的高度和国家Geoff

name = 'Geoff' h = 177 c= US

有谁知道搜索和finiding这个最有效的方法?谢谢!

标签: pythonpandas

解决方案


或者,您也可以使用 df.query

df.query('Name == "Geoff"') # To get an entire row based on the filter condition
df.query('Name == "Geoff"').Height # For a specific column value (Height in this case) based on the filter
# or
df.query('Name == "Geoff"')['Height']


推荐阅读