熊猫根据索引检索值

问题描述 投票:0回答:2

曾经尝试搜索此内容,但似乎无法找到正确的答案。

给出以下简单数据框:

   country   continent    population
0    UK       Europe        111111
1   Spain     Europe        222222
2   Malaysia   Asia         333333
3    USA      America       444444

如果有条件在哪里给出索引值,如何检索国家/地区值?例如,如果给我的索引值为2,我应该返回马来西亚。

编辑:忘记提及输入索引值来自一个变量(考虑到用户选择特定行,并且所选行提供了索引值变量)。

谢谢。

pandas
2个回答
0
投票

df.iloc [2] ['国家/地区]。

iloc用于按位置选择,请参见pandas.DataFrame.iloc documentation了解更多选项。


0
投票
index = 2    
print(df.iloc[index]['country'])

Malaysia
© www.soinside.com 2019 - 2024. All rights reserved.