问题:如何从数据框的单元格获取值?

我构造了一个条件,可以从我的数据帧中准确提取一行:

d2 = df[(df['l_ext']==l_ext) & (df['item']==item) & (df['wn']==wn) & (df['wd']==1)]

现在,我想从特定列中获取一个值:

val = d2['col_name']

但是结果是我得到一个包含一行一列(一个单元格)的数据帧。这不是我所需要的。我需要一个值(一个浮点数)。我该如何在熊猫中做到这一点?

I have constructed a condition that extract exactly one row from my data frame:

d2 = df[(df['l_ext']==l_ext) & (df['item']==item) & (df['wn']==wn) & (df['wd']==1)]

Now I would like to take a value from a particular column:

val = d2['col_name']

But as a result I get a data frame that contains one row and one column (i.e. one cell). It is not what I need. I need one value (one float number). How can I do it in pandas?


回答 0

如果您的DataFrame仅包含一行,则使用iloc,以Series的形式访问第一(唯一)行,然后使用列名访问值:

In [3]: sub_df
Out[3]:
          A         B
2 -0.133653 -0.030854

In [4]: sub_df.iloc[0]
Out[4]:
A   -0.133653
B   -0.030854
Name: 2, dtype: float64

In [5]: sub_df.iloc[0]['A']
Out[5]: -0.13365288513107493

If you have a DataFrame with only one row, then access the first (only) row as a Series using iloc, and then the value using the column name:

In [3]: sub_df
Out[3]:
          A         B
2 -0.133653 -0.030854

In [4]: sub_df.iloc[0]
Out[4]:
A   -0.133653
B   -0.030854
Name: 2, dtype: float64

In [5]: sub_df.iloc[0]['A']
Out[5]: -0.13365288513107493

回答 1

这些是标量的快速访问

In [15]: df = pandas.DataFrame(numpy.random.randn(5,3),columns=list('ABC'))

In [16]: df
Out[16]: 
          A         B         C
0 -0.074172 -0.090626  0.038272
1 -0.128545  0.762088 -0.714816
2  0.201498 -0.734963  0.558397
3  1.563307 -1.186415  0.848246
4  0.205171  0.962514  0.037709

In [17]: df.iat[0,0]
Out[17]: -0.074171888537611502

In [18]: df.at[0,'A']
Out[18]: -0.074171888537611502

These are fast access for scalars

In [15]: df = pandas.DataFrame(numpy.random.randn(5,3),columns=list('ABC'))

In [16]: df
Out[16]: 
          A         B         C
0 -0.074172 -0.090626  0.038272
1 -0.128545  0.762088 -0.714816
2  0.201498 -0.734963  0.558397
3  1.563307 -1.186415  0.848246
4  0.205171  0.962514  0.037709

In [17]: df.iat[0,0]
Out[17]: -0.074171888537611502

In [18]: df.at[0,'A']
Out[18]: -0.074171888537611502

回答 2

您可以将1×1数据帧转换为numpy数组,然后访问该数组的第一个也是唯一的值:

val = d2['col_name'].values[0]

You can turn your 1×1 dataframe into a numpy array, then access the first and only value of that array:

val = d2['col_name'].values[0]

回答 3

多数答案都在使用iloc,这对于按位置选择非常有用。

如果需要按标签选择 loc会更方便。

用于显式获取值(等于不推荐使用的df.get_value(’a’,’A’))

# this is also equivalent to df1.at['a','A']
In [55]: df1.loc['a', 'A'] 
Out[55]: 0.13200317033032932

Most answers are using iloc which is good for selection by position.

If you need selection-by-label loc would be more convenient.

For getting a value explicitly (equiv to deprecated df.get_value(‘a’,’A’))

# this is also equivalent to df1.at['a','A']
In [55]: df1.loc['a', 'A'] 
Out[55]: 0.13200317033032932

回答 4

我需要一个由列和索引名称选择的单元格的值。此解决方案为我工作:

original_conversion_frequency.loc[1,:].values[0]

I needed the value of one cell, selected by column and index names. This solution worked for me:

original_conversion_frequency.loc[1,:].values[0]


回答 5

熊猫10.1 / 13.1之后看起来像变化

在iloc不可用之前,我从10.1升级到13.1。

现在有了13.1,iloc[0]['label']将获得单个值数组而不是标量。

像这样:

lastprice=stock.iloc[-1]['Close']

输出:

date
2014-02-26 118.2
name:Close, dtype: float64

It looks like changes after pandas 10.1/13.1

I upgraded from 10.1 to 13.1, before iloc is not available.

Now with 13.1, iloc[0]['label'] gets a single value array rather than a scalar.

Like this:

lastprice=stock.iloc[-1]['Close']

Output:

date
2014-02-26 118.2
name:Close, dtype: float64

回答 6

我找到的最快/最简单的选项如下。501表示行索引。

df.at[501,'column_name']
df.get_value(501,'column_name')

The quickest/easiest options I have found are the following. 501 represents the row index.

df.at[501,'column_name']
df.get_value(501,'column_name')

回答 7

对于0.10的大熊猫,在iloc不可取的地方,过滤a DF并获取列的第一行数据VALUE

df_filt = df[df['C1'] == C1val & df['C2'] == C2val]
result = df_filt.get_value(df_filt.index[0],'VALUE')

如果过滤的行数超过1,则获取第一行的值。如果过滤器导致数据帧为空,则会出现异常。

For pandas 0.10, where iloc is unavalable, filter a DF and get the first row data for the column VALUE:

df_filt = df[df['C1'] == C1val & df['C2'] == C2val]
result = df_filt.get_value(df_filt.index[0],'VALUE')

if there is more then 1 row filtered, obtain the first row value. There will be an exception if the filter result in empty data frame.


回答 8

不知道这是否是一个好习惯,但是我注意到我也可以通过将序列强制转换为来获得值float

例如

rate

3 0.042679

名称:Unemployment_rate,dtype:float64

float(rate)

0.0426789

Not sure if this is a good practice, but I noticed I can also get just the value by casting the series as float.

e.g.

rate

3 0.042679

Name: Unemployment_rate, dtype: float64

float(rate)

0.0426789


回答 9

它不需要很复杂:

val = df.loc[df.wd==1, 'col_name'].values[0]

It doesn’t need to be complicated:

val = df.loc[df.wd==1, 'col_name'].values[0]

回答 10

df_gdp.columns

索引([u’Country’,u’Country Code’,u’Indicator Name’,u’Indicator Code’,u’1960’,u’1961’,u’1962’,u’1963’,u’1964′ ,u’1965’,u’1966’,u’1967’,u’1968’,u’1969’,u’1970’,u’1971’,u’1972’,u’1973’,u’1974′ ,u’1975’,u’1976’,u’1977’,u’1978’,u’1979’,u’1980’,u’1981’,u’1982’,u’1983’,u’1984′ ,u’1985’,u’1986’,u’1987’,u’1988’,u’1989’,u’1990’,u’1991’,u’1992’,u’1993’,u’1994′ ,u’1995’,u’1996’,u’1997’,u’1998’,u’1999’,u’2000’,u’2001’,u’2002’,u’2003’,u’2004’,u’2005’,u’2006’,u’2007’,u’2008’,u’2009’,u’2010’, u’2011’,u’2012’,u’2013’,u’2014’,u’2015’,u’2016′],dtype =’object’)

df_gdp[df_gdp["Country Code"] == "USA"]["1996"].values[0]

8100000000000.0

df_gdp.columns

Index([u’Country’, u’Country Code’, u’Indicator Name’, u’Indicator Code’, u’1960′, u’1961′, u’1962′, u’1963′, u’1964′, u’1965′, u’1966′, u’1967′, u’1968′, u’1969′, u’1970′, u’1971′, u’1972′, u’1973′, u’1974′, u’1975′, u’1976′, u’1977′, u’1978′, u’1979′, u’1980′, u’1981′, u’1982′, u’1983′, u’1984′, u’1985′, u’1986′, u’1987′, u’1988′, u’1989′, u’1990′, u’1991′, u’1992′, u’1993′, u’1994′, u’1995′, u’1996′, u’1997′, u’1998′, u’1999′, u’2000′, u’2001′, u’2002′, u’2003′, u’2004′, u’2005′, u’2006′, u’2007′, u’2008′, u’2009′, u’2010′, u’2011′, u’2012′, u’2013′, u’2014′, u’2015′, u’2016′], dtype=’object’)

df_gdp[df_gdp["Country Code"] == "USA"]["1996"].values[0]

8100000000000.0


声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。