Advertisement
celestialgod

Pandas Groupby take first row

Aug 26th, 2018
340
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.31 KB | None | 0 0
  1. import pandas as pd
  2. import numpy as np
  3. df = pd.DataFrame({'id': np.array([1,1,2,3,3,3]),
  4.                    'no': np.array([1,2,3,0,1,2]),
  5.                    'col1': np.random.randn(6),
  6.                    'col2': np.random.randn(6)})
  7. df = df.sort_values(['id', 'no'], ascending=[1, 0])
  8. df.groupby('id').first()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement