Pandas core groupby DataFrameGroupBy function nth() in Python
The Pandas core groupby DataFrameGroupBy function nth() used to retrieve the nth row from each group, if n is an integer, otherwise a subset of rows.
It can be either a call or an index but dropna is not available with index notation. An index notation allows a comma separated list of integers and slices.
Syntax
 1 DataFrameGroupBy.nth()
n : It is an integer, slice or list of integers and slices that represents a single nth value for the row or a list of nth values or slices.
dropna : It can be any, all or None that apply the specified dropna operation before counting which row is the nth row. Only supported if n is an integer. If not specified, the default value will be None.
nth() function
 1 import pandas as pd
 2 import numpy as np
 3 
 4 df = pd.DataFrame({'A' : ['A1', 'A2', 'A2', 'A1', 'A3', 'A2', 'A3'],
 5 			'B' : [1, np.nan, 6, 3, 5, np.nan, 8],
 6 			'C' : [3, 6, np.nan, 3, 5, -3, np.nan]})
 7 
 8 grouped = df.groupby('A')
 9 res = grouped.nth(1)
 10 
 11 print('The 1st row from each group :')
 12 print(res)
In the above example, a DataFrame object is created by passing a dictionary. A DataFrameGroupBy function nth() function is called by passing an integer 1 that returns a 1st row from each group(second row as n starts from 0). The result is assign to the variable that will be printed on console.
Output
 1 The 1st row from each group :
 2     A    B    C
 3 2  A2  6.0  NaN
 4 3  A1  3.0  3.0
 5 6  A3  8.0  NaN

nth() with negative

nth() with negative
 1 import pandas as pd
 2 import numpy as np
 3 
 4 df = pd.DataFrame({'A' : ['A1', 'A2', 'A2', 'A1', 'A3', 'A2', 'A3'],
 5 			'B' : [1, np.nan, 6, 3, 5, np.nan, 8],
 6 			'C' : [3, 6, np.nan, 3, 5, -3, np.nan]})
 7 
 8 grouped = df.groupby('A')
 9 # groups - 1 row
 10 res = grouped.nth(-1)
 11 
 12 print('The resulting rows from each group :')
 13 print(res)
Output
 1 The resulting rows from each group :
 2     A    B    C
 3 3  A1  3.0  3.0
 4 5  A2  NaN -3.0
 5 6  A3  8.0  NaN

nth() with slice()

nth() with slice()
 1 import pandas as pd
 2 import numpy as np
 3 
 4 df = pd.DataFrame({'A' : ['A1', 'A2', 'A2', 'A1', 'A3', 'A2', 'A3'],
 5 			'B' : [1, np.nan, 6, 3, 5, np.nan, 8],
 6 			'C' : [3, 6, np.nan, 3, 5, -3, np.nan]})
 7 
 8 grouped = df.groupby('A')
 9 res = grouped.nth(slice(None, -1))
 10 
 11 print('The resulting rows from each group :')
 12 print(res)
Output
 1 The resulting rows from each group :
 2     A    B    C
 3 0  A1  1.0  3.0
 4 1  A2  NaN  6.0
 5 2  A2  6.0  NaN
 6 4  A3  5.0  5.0

nth() with Series

nth() with Series
 1 import pandas as pd
 2 import numpy as np
 3 
 4 idx = ['A1', 'A2', 'A2', 'A1', 'A3', 'A2', 'A3']
 5 ser = pd.Series([3, 6, np.nan, 3, 5, 3, 2], index = idx)
 6 
 7 grouped = ser.groupby(ser.index)
 8 # or 
 9 # grouped = ser.groupby(level = 0)
 10 res = grouped.nth(1)
 11 
 12 print('The 1st row from each group :')
 13 print(res)
In the above example, a SeriesGroupBy function nth() function is called by passing an integer 1 that returns a 1st row from each group(second row as n starts from 0).
Output
 1 The 1st row from each group :
 2 A2    NaN
 3 A1    3.0
 4 A3    2.0
 5 dtype: float64
Privacy Policy
Terms of Service
Disclaimer
Contact us
About us