在不知道索引的情况下获取Series的第一个元素 [英] Get first element of Series without knowing the index

查看:754
本文介绍了在不知道索引的情况下获取Series的第一个元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在没有索引信息的情况下,我可以以任何方式获得Seires的第一个元素.

Is that any way that I can get first element of Seires without have information on index.

例如,我们有一个系列

    import pandas as pd
    key='MCS096'
    SUBJECTS=pd.DataFrame({'ID':Series([146],index=[145]),\
                   'study':Series(['MCS'],index=[145]),\
                   'center':Series(['Mag'],index=[145]),\
                   'initials':Series(['MCS096'],index=[145])
                   })

打印出主题:

    print (SUBJECTS[SUBJECTS.initials==key]['ID'])
    145    146
    Name: ID, dtype: int64

如何在不使用索引145的情况下获得146的值?

How can I get the value here 146 without using index 145?

非常感谢

推荐答案

使用iloc按位置(而不是标签)进行访问:

Use iloc to access by position (rather than label):

In [11]: df = pd.DataFrame([[1, 2], [3, 4]], ['a', 'b'], ['A', 'B'])

In [12]: df
Out[12]: 
   A  B
a  1  2
b  3  4

In [13]: df.iloc[0]  # first row in a DataFrame
Out[13]: 
A    1
B    2
Name: a, dtype: int64

In [14]: df['A'].iloc[0]  # first item in a Series (Column)
Out[14]: 1

这篇关于在不知道索引的情况下获取Series的第一个元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆