返回 Pandas 数据框中特定列的 int 值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40641511/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 02:27:04  来源:igfitidea点击:

Return the int value for a specific column in a pandas data frame

pythonpandasnumpy

提问by HimanAB

In my data frame DF I have a column called 'A' and its values are integer values. However when I want to retrieve the value of A for a specific row using

在我的数据框 DF 中,我有一个名为“A”的列,它的值是整数值。但是,当我想使用以下方法检索特定行的 A 值时

DF[DF.someCondition=condition].A

it returns an object of shape (1,) that is not int because int does not have a shape. I want int because I want to use this value as an index entry to another numpy array. How can I retrieve the value of A so that it's an int value?

它返回一个形状为 (1,) 的对象,该对象不是 int ,因为 int 没有形状。我想要 int 因为我想使用这个值作为另一个 numpy 数组的索引条目。如何检索 A 的值使其成为 int 值?

回答by unutbu

In general, a condition of the form

一般来说,表格的条件

DF.someCondition = condition

may be True more than once. That is why

可能不止一次为真。这就是为什么

DF[DF.someCondition=condition].A

returns an object of shape (1,)rather than a scalar value. If you are certain that the condition is True only once, then you can extract the scalar value using item

返回形状对象(1,)而不是标量值。如果您确定条件只为 True 一次,则可以使用以下方法提取标量值item

DF[DF.someCondition=condition].A.item()

However, as MaxU suggested, it is better to use .locto avoid chained-indexing:

但是,正如MaxU 建议的那样,最好使用.loc以避免链接索引:

DF.loc[DF.someCondition=condition, 'A'].item()


For example,

例如,

import numpy as np
import pandas as pd

df = pd.DataFrame(np.arange(6).reshape(3,2), columns=list('AB'))
df[df['B']==3].A
# 1    2
# Name: A, dtype: int64

df.loc[df['B']==3, 'A'].item()
# 2

回答by smkj33

For future reference:

备查:

Using iteras suggested by unutbu definitely gets the job done; however it must be noted that the function has been deprecated as of 2018.

iter按照 unutbu 的建议使用肯定可以完成工作;但是必须注意的是,该功能已于 2018 年弃用。

Using the iterfunction as suggested in unutbu's answer will result in a warning that looks as below:

使用iterunutbu's answer 中建议的函数将导致警告,如下所示:

FutureWarning: itemhas been deprecated and will be removed in a future version

FutureWarning:item已弃用,将在未来版本中删除

As is evident from the warning, this functionality will soon removed. This information can also be found in the sourcecode.

从警告中可以明显看出,此功能将很快删除。这些信息也可以在代码中找到。

Source Screenshot

源截图

As posted herefollowing is the workaround using iterwith nextif the first matched value is required:

张贴在这里下面是使用变通方法iternext是否需要第一个匹配值:

x = df.loc[df['B']==3, 'A'].item()
next(iter(x), 'no match')

The advantage is that if no value is matched the default value can be returned.

优点是如果没有匹配的值,则可以返回默认值。



In case you are further interested, this issue is discussed hereand here.

如果您有兴趣,可以在此处此处讨论此问题。