问题:如何从Pandas DataFrame获取值而不是索引和对象类型

说我有以下DataFrame

字母编号
A 1
B 2
C 3
4天

可以通过以下代码获得

import pandas as pd

letters=pd.Series(('A', 'B', 'C', 'D'))
numbers=pd.Series((1, 2, 3, 4))
keys=('Letters', 'Numbers')
df=pd.concat((letters, numbers), axis=1, keys=keys)

现在,我想从“字母”列中获取值C。

命令行

df[df.Letters=='C'].Letters

将返回

2℃
名称:字母,dtype:对象

我怎样才能只获得C值而不是整个两行输出?

Say I have the following DataFrame

Letter    Number
A          1
B          2
C          3
D          4

Which can be obtained through the following code

import pandas as pd

letters=pd.Series(('A', 'B', 'C', 'D'))
numbers=pd.Series((1, 2, 3, 4))
keys=('Letters', 'Numbers')
df=pd.concat((letters, numbers), axis=1, keys=keys)

Now I want to get the value C from the column Letters.

The command line

df[df.Letters=='C'].Letters

will return

2    C
Name: Letters, dtype: object

How can I get only the value C and not the whole two line output?


回答 0

df[df.Letters=='C'].Letters.item()

这将返回从该选择返回的索引/系列中的第一个元素。在这种情况下,该值始终是第一个元素。

编辑:

或者,您可以运行loc()并以这种方式访问​​第一个元素。这比较短,这是我过去实现它的方式。

df[df.Letters=='C'].Letters.item()

This returns the first element in the Index/Series returned from that selection. In this case, the value is always the first element.

EDIT:

Or you can run a loc() and access the first element that way. This was shorter and is the way I have implemented it in the past.


回答 1

使用values属性将值作为np数组返回,然后使用[0]获取第一个值:

In [4]:
df.loc[df.Letters=='C','Letters'].values[0]

Out[4]:
'C'

编辑

我个人更喜欢使用下标运算符访问列:

df.loc[df['Letters'] == 'C', 'Letters'].values[0]

这样可以避免列名中可以包含空格或破折号的问题-,这意味着使用进行访问.

Use the values attribute to return the values as a np array and then use [0] to get the first value:

In [4]:
df.loc[df.Letters=='C','Letters'].values[0]

Out[4]:
'C'

EDIT

I personally prefer to access the columns using subscript operators:

df.loc[df['Letters'] == 'C', 'Letters'].values[0]

This avoids issues where the column names can have spaces or dashes - which mean that accessing using ..


回答 2

import pandas as pd

dataset = pd.read_csv("data.csv")
values = list(x for x in dataset["column name"])

>>> values[0]
'item_0'

编辑:

实际上,您可以像对任何旧数组一样索引数据集。

import pandas as pd

dataset = pd.read_csv("data.csv")
first_value = dataset["column name"][0]

>>> print(first_value)
'item_0'
import pandas as pd

dataset = pd.read_csv("data.csv")
values = list(x for x in dataset["column name"])

>>> values[0]
'item_0'

edit:

actually, you can just index the dataset like any old array.

import pandas as pd

dataset = pd.read_csv("data.csv")
first_value = dataset["column name"][0]

>>> print(first_value)
'item_0'

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