[英]Return cell value in Pandas multi-index dataframe as string
I am trying to return a single cell value from a dataframe of about 11.000 rows and 10 columns: 我正在尝试从大约11.000行和10列的数据框中返回单个单元格值:
df = pd.DataFrame({ 'A' : ('foo', 'foo', 'bar', 'bar'),
'B' : ('horse', 'car', 'horse', 'car'),
'C' : ('red', 'green', 'blue', 'black')})
df
A B C
0 foo horse red
1 foo car green
2 bar horse blue
3 bar car black
I've tried a few versions of loc, but always get an object returned, not the actual value ('green'). 我尝试了loc的几个版本,但总是得到一个返回的对象,而不是实际值(“绿色”)。 For instance:
例如:
df.loc[(df['A'] == 'foo') & (df['B'] == 'car'), 'C']
returns: 收益:
1 green
Name: C, dtype: object
The result is correct, but it is returned as an object, not as a string. 结果是正确的,但它作为对象而不是字符串返回。 I also tried setting columns A and B as indices in a multi-index frame.
我还尝试将A和B列设置为多索引框架中的索引。 The loc code is then easier, but same result.
位置代码会更容易,但结果相同。
So how can I get the value as a string, not as an object? 那么如何将值作为字符串而不是作为对象来获取呢?
You are getting a series out, and you want the first object. 您正在制作系列,想要第一个对象。
You can think of what you are doing as getting the pd.Series equivalent of a list of length 1: ['green']
. 您可以将pd.Series等效为长度为1的列表:
['green']
。 If it was a list, you would do list[0]
. 如果是列表,则将执行
list[0]
。
However, pandas series subset based on index, so you can't do this. 但是,pandas系列子集基于索引,因此您无法执行此操作。 To subset without knowledge of a series index, use
iloc[0]
: 要在不知道序列索引的情况下进行子集化,请使用
iloc[0]
:
df.loc[(df['A'] == 'foo') & (df['B'] == 'car'), 'C'].iloc[0]
'green'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.