How to get the last N rows of a pandas DataFrame?

前端 未结 3 2027
夕颜
夕颜 2020-11-29 22:46

I have pandas dataframe df1 and df2 (df1 is vanila dataframe, df2 is indexed by \'STK_ID\' & \'RPT_Date\') :

>>> df1
          


        
相关标签:
3条回答
  • 2020-11-29 23:10

    This is because of using integer indices (ix selects those by label over -3 rather than position, and this is by design: see integer indexing in pandas "gotchas"*).

    *In newer versions of pandas prefer loc or iloc to remove the ambiguity of ix as position or label:

    df.iloc[-3:]
    

    see the docs.

    As Wes points out, in this specific case you should just use tail!

    0 讨论(0)
  • 2020-11-29 23:21

    Don't forget DataFrame.tail! e.g. df1.tail(10)

    0 讨论(0)
  • 2020-11-29 23:32

    How to get the last N rows of a pandas DataFrame?

    If you are slicing by position, __getitem__ (i.e., slicing with[]) works well, and is the most succinct solution I've found for this problem.

    pd.__version__
    # '0.24.2'
    
    df = pd.DataFrame({'A': list('aaabbbbc'), 'B': np.arange(1, 9)})
    df
    
       A  B
    0  a  1
    1  a  2
    2  a  3
    3  b  4
    4  b  5
    5  b  6
    6  b  7
    7  c  8
    

    df[-3:]
    
       A  B
    5  b  6
    6  b  7
    7  c  8
    

    This is the same as calling df.iloc[-3:], for instance (iloc internally delegates to __getitem__).


    As an aside, if you want to find the last N rows for each group, use groupby and GroupBy.tail:

    df.groupby('A').tail(2)
    
       A  B
    1  a  2
    2  a  3
    5  b  6
    6  b  7
    7  c  8
    
    0 讨论(0)
提交回复
热议问题