How to get value of first row of a given column in pandas ?

How to get value of first row of a given column in pandas ?

Asked on December 14, 2018 in Pandas.
Add Comment


  • 2 Answer(s)

    The given problem can be solved as :

    For selecting the ith row, use iloc:

    In [31]: df_test.iloc[0]
    Out[31]:
    ATime       1.2
    X           2.0
    Y           15.0
    Z           2.0
    Btime       1.2
    C           12.0
    D           25.0
    E           12.0
    Name: 0, dtype:float64
    

    For selecting the ith value in the Btime column you could use:

    In [30]: df_test['Btime'].iloc[0]
    Out[30]: 1.2
    

    It is important to note that the code df_test.ix[i, ‘Btime’] may give you the ith value since ix will try to index by label before position. So if the DataFrame has an integer index which is not in sorted order which begins from 0, then using ix[i] will return the row labeled i. For example,

    In [1]: df = pd.DataFrame({'foo':list('ABC')}, index=[0,2,1])
    In [2]: df
    Out[2]: 
    foo
    0   A
    2   B
    1   C
    In [4]: df.ix[1, 'foo']
    Out[4]: 'C'
    
    Answered on December 14, 2018.
    Add Comment

    Try this instead.,

      1. df.iloc[0].head(1) – First data set only from entire first row.
      2. df.iloc[0] – Entire First row in column.

     

    Answered on December 14, 2018.
    Add Comment


  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.