[英]Get cell value from a pandas DataFrame row
I am new to pandas and have what I think should be a simple question.我是熊猫的新手,我认为应该是一个简单的问题。 I have a simple DataFrame and I need to be able to get the value of a specific cell.我有一个简单的 DataFrame,我需要能够获取特定单元格的值。 Here is a sample of my DataFrame:这是我的 DataFrame 示例:
>>> airports.sample(5)
iata name city state country
2144 M15 Perry County Linden TN USA
2391 N69 Stormville Stormville NY USA
861 ARA Acadiana Regional New Iberia LA USA
2596 PDX Portland Intl Portland OR USA
3238 VES Darke County Versailles OH USA
As an example of what I am trying to do, I need to get the value of the name column for the row where the iata column == 'PDX'.作为我想要做的一个例子,我需要获取iata列 == 'PDX' 所在行的名称列的值。 I can use the following syntax to retrieve the row I want:我可以使用以下语法来检索我想要的行:
>>> airports[airports.iata == 'PDX'].name
2596 Portland Intl
Name: name, dtype: object
The question is now, how do I retrieve the value of the name cell for this row?现在的问题是,如何检索该行的名称单元格的值?
.loc
returns series when you do boolean indexing because there are chances that you have multiple match cases. .loc
在您进行布尔索引时返回系列,因为您可能有多个匹配案例。
df.loc[df.iata == 'PDX','name']
2596 Portland Intl
Name: name, dtype: object
If you want only the value then convert it to list and then access according to the index如果您只想要该值,则将其转换为列表,然后根据索引进行访问
df.loc[df.iata == 'PDX','name'].tolist()[0]
Or access based on the index in values ie或基于值中的索引进行访问,即
df.loc[df.iata == 'PDX','name'].values[0]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.