pandas 如何检索pandas Series对象中第n个元素的值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36710702/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 01:04:20  来源:igfitidea点击:

How to retrieve value of n-th element in pandas Series object?

pythonpandasindexing

提问by Moppentapper

I have a series object (1 column of a DataFrame) and would like to extract the value of the first element. Is there a way to do this simply without converting to a list and without knowing the key? Or is the only way to access it by converting it to a list first using tolist()[n]?

我有一个系列对象( a 的 1 列DataFrame)并想提取第一个元素的值。有没有办法在不转换为列表且不知道密钥的情况下简单地做到这一点?或者是通过首先使用将其转换为列表来访问它的唯一方法tolist()[n]

回答by jezrael

I think you can use iloc:

我认为你可以使用iloc

print df
  col
0   a
1   b
2   c
3   d
4   e

print df.iloc[0]
col    a
Name: 0, dtype: object