Get first element of Series without knowing the index [duplicate]

本小妞迷上赌 提交于 2019-11-27 14:18:10

问题


This question already has an answer here:

  • Pandas - Get first row value of a given column 7 answers

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

For example,We have a Series

    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])
                   })

prints out SUBJECTS:

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

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

Thank you very much


回答1:


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


来源:https://stackoverflow.com/questions/24273130/get-first-element-of-series-without-knowing-the-index

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!