SHARE
TWEET

Untitled

a guest Jun 26th, 2019 60 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import pandas as pd
  2.  
  3. mi = pd.MultiIndex(
  4.     levels=[['group1', 'group2'], [1,2,3,4]],
  5.     codes=[[0,0,1,1], [0,1,2,3]],
  6.     names=['group', 'id']
  7. )
  8.  
  9. df = pd.DataFrame({
  10.     'col1': [400, 300, 200, 100],
  11.     'col2': [10, 20, 30, 40],
  12. }, index=mi)
  13.  
  14. max_idx = df.groupby(level='group').idxmax()
  15.  
  16. ret = max_idx.apply(lambda d:d.transform(lambda d:df.loc[d]).stack(), axis=1)
  17. print(ret)
  18. #       col1      col2
  19. #       col1 col2 col1 col2
  20. #group
  21. #group1  400   10  300   20
  22. #group2  200   30  100   40
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top