简体   繁体   English

从 Pandas DataFrame 行获取单元格值

[英]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.

相关问题 如何从 Pandas DataFrame 中的一行中获取值? - How to get value from a row in Pandas DataFrame? 如果单元格内容是一个集合并且我想查看其中是否有值,如何从熊猫数据框中有条件地获取一行? - How to conditionally get a row from a pandas dataframe, if the cell content is a set and I want to see if a value is in it? 从pandas DataFrame中采样一行后,如何获取一个单元格的值? - Once I sample a row from a pandas DataFrame, how can I get a value of one cell? 根据单元格值从Pandas DataFrame删除行 - Delete Row from Pandas DataFrame based on cell value 在 pandas dataframe 中获取 t-1 值(来自上一个单元格) - Get t-1 value (from previous cell) in a pandas dataframe 从特定列和行获取值到变量 - Pandas DataFrame - get a value from a specific column and row to a variable - Pandas DataFrame 从Pandas DataFrame获取最大值的行索引和列索引 - Get the row index and column index of maximum value from Pandas DataFrame 如何获取熊猫数据框中单元格值的长度? - How to get the length of a cell value in pandas dataframe? 如何在 Pandas 数据框中按值获取精确的行号和列号,即单元格地址 - How can I get exact row and column number i.e cell address by value in Pandas dataframe 如何从 Pandas 中的 Dataframe 中提取单元格值 - How to extract a cell value from a Dataframe in Pandas
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM