从数据框中选择特定值 [英] selecting a specific value from a data frame

查看:67
本文介绍了从数据框中选择特定值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从数据框中选择一个值.但问题是输出带有数据类型和列名.这是我从 csv 文件中读取的数据框,

I am trying to select a value from a dataframe. But the problem is the output is with data type and column name. Here is my data frame which i am reading from a csv file,

Name,Code
blackberry,1
wineberry,2
rasberry,1
blueberry,1
mulberry,2

这是我的测试代码-

dataFrame=pd.read_csv("test.csv")
value = dataFrame.loc[dataFrame['Name'] == 'rasberry']['Code']
print(value)
strvalue=str(value)
if(strvalue=="1"):
    print("got it")

value 的预期输出将是 1 但它是

The expected ouput of value would be 1 but it is

2  1\nName: Code, dtype: int64

这就是 if 条件不起作用的原因.我怎样才能得到具体的价值?我正在使用 pandas

and that's why the if condition is not working. How can I get the specific value? I am using pandas

推荐答案

你得到的 value 是一个 Series 对象.您可以使用 .iloc 从中提取值:

The value you get is a Series object. You can use .iloc to extract the value from it:

value.iloc[0]
# 1

或者你可以使用 .values 来提取底层的 numpy 数组,然后使用 index 来提取值:

Or you can use .values to extract the underlying numpy array and then use index to extract the value:

value.values[0]
# 1

这篇关于从数据框中选择特定值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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