daily pastebin goal
44%
SHARE
TWEET

Untitled

a guest Jan 18th, 2019 57 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. >>> df = pd.DataFrame(np.ones((10, 3)), columns=list("ABC"))
  2. >>> df.index = pd.MultiIndex.from_product([range(5), list("AB")])
  3. >>> df
  4.        A    B    C
  5. 0 A  1.0  1.0  1.0
  6.   B  1.0  1.0  1.0
  7. 1 A  1.0  1.0  1.0
  8.   B  1.0  1.0  1.0
  9. 2 A  1.0  1.0  1.0
  10.   B  1.0  1.0  1.0
  11. 3 A  1.0  1.0  1.0
  12.   B  1.0  1.0  1.0
  13. 4 A  1.0  1.0  1.0
  14.   B  1.0  1.0  1.0
  15.  
  16. >>> values = ["B", "A"]
  17. >>> idx = pd.IndexSlice
  18. >>> subset = df.loc[idx[:, values], values]
  19. >>> subset
  20.        B    A
  21. 0 A  1.0  1.0
  22.   B  1.0  1.0
  23. 1 A  1.0  1.0
  24.   B  1.0  1.0
  25. 2 A  1.0  1.0
  26.   B  1.0  1.0
  27. 3 A  1.0  1.0
  28.   B  1.0  1.0
  29. 4 A  1.0  1.0
  30.   B  1.0  1.0
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