如何从数据框的单元格获取值? [英] How to get a value from a cell of a dataframe?

查看:62
本文介绍了如何从数据框的单元格获取值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

推荐答案

如果您的DataFrame仅包含一行,则使用iloc作为Series来访问第一行(唯一),然后使用该列访问值名称:

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

这篇关于如何从数据框的单元格获取值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆