说我有以下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值而不是整个两行输出?
问题:如何从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'
本文由 Python 实用宝典 作者:Python实用宝典 发表,其版权均为 Python 实用宝典 所有,文章内容系作者个人观点,不代表 Python 实用宝典 对观点赞同或支持。如需转载,请注明文章来源。