Guest User

Untitled

a guest
Dec 13th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.45 KB | None | 0 0
  1. >>> import numpy as np
  2. >>> import pandas as pd
  3. >>> pd.__version__
  4. u'0.23.4'
  5. >>> index = pd.MultiIndex(levels=[[u'bar', u'baz', u'foo', u'qux'],
  6. [u'one', u'two', u'three', u'four']],
  7. labels=[[0, 0, 1, 1, 2, 2, 3, 3],
  8. [0, 0, 1, 1, 2, 2, 3, 3]],
  9. names=[u'first', u'second'])
  10. >>> df = pd.DataFrame(np.random.randn(8, 4), index=index)
  11. >>> df.columns = ['c0', 'c1', 'c2', 'c3']
  12. >>> df
  13. c0 c1 c2 c3
  14. first second
  15. bar one -2.366973 -0.887149 -0.301309 1.312207
  16. one 1.266500 0.864888 -1.407567 0.265077
  17. baz two -1.926091 -0.671274 -0.295846 0.679759
  18. two -0.212970 0.136552 0.219074 0.541827
  19. foo three -0.698288 -2.059952 0.248811 0.947879
  20. three -2.017481 0.163013 -0.906551 -0.102474
  21. qux four -1.083530 0.097077 0.224977 0.251739
  22. four 0.943804 1.356789 -0.953357 0.592986
  23.  
  24. >>> two_data = df[df.index.get_level_values('second') == 'two']
  25. >>> mask = (two_data['c1'] > 0)
  26. >>> mask = mask.values
  27. array([False, True])
  28.  
  29. >>> df[df.index.get_level_values('second') == 'two'][mask].drop('two', level=1)
  30. Empty DataFrame
  31. Columns: [c0, c1, c2, c3]
  32. Index: []
  33. >>> df[df.index.get_level_values('second') == 'two'].iloc[mask].drop('two', level=1)
  34. Empty DataFrame
  35. Columns: [c0, c1, c2, c3]
  36. Index: []
  37.  
  38. >>> df
  39. c0 c1 c2 c3
  40. first second
  41. bar one -2.366973 -0.887149 -0.301309 1.312207
  42. one 1.266500 0.864888 -1.407567 0.265077
  43. baz two -1.926091 -0.671274 -0.295846 0.679759
  44. two -0.212970 0.136552 0.219074 0.541827
  45. foo three -0.698288 -2.059952 0.248811 0.947879
  46. three -2.017481 0.163013 -0.906551 -0.102474
  47. qux four -1.083530 0.097077 0.224977 0.251739
  48. four 0.943804 1.356789 -0.953357 0.592986
  49.  
  50. >>> df[df.index.get_level_values('second') == 'two'][mask].drop('two', level=1, inplace=True)
  51. >>> df
  52. c0 c1 c2 c3
  53. first second
  54. bar one -2.366973 -0.887149 -0.301309 1.312207
  55. one 1.266500 0.864888 -1.407567 0.265077
  56. baz two -1.926091 -0.671274 -0.295846 0.679759
  57. two -0.212970 0.136552 0.219074 0.541827
  58. foo three -0.698288 -2.059952 0.248811 0.947879
  59. three -2.017481 0.163013 -0.906551 -0.102474
  60. qux four -1.083530 0.097077 0.224977 0.251739
  61. four 0.943804 1.356789 -0.953357 0.592986
  62.  
  63. >>> df[df.index.get_level_values('second') == 'two'].iloc[mask].drop('two', level=1, inplace=True)
  64. >>> df
  65. c0 c1 c2 c3
  66. first second
  67. bar one -2.366973 -0.887149 -0.301309 1.312207
  68. one 1.266500 0.864888 -1.407567 0.265077
  69. baz two -1.926091 -0.671274 -0.295846 0.679759
  70. two -0.212970 0.136552 0.219074 0.541827
  71. foo three -0.698288 -2.059952 0.248811 0.947879
  72. three -2.017481 0.163013 -0.906551 -0.102474
  73. qux four -1.083530 0.097077 0.224977 0.251739
  74. four 0.943804 1.356789 -0.953357 0.592986
  75.  
  76. c0 c1 c2 c3
  77. first second
  78. bar one -2.366973 -0.887149 -0.301309 1.312207
  79. one 1.266500 0.864888 -1.407567 0.265077
  80. baz two -1.926091 -0.671274 -0.295846 0.679759
  81. foo three -0.698288 -2.059952 0.248811 0.947879
  82. three -2.017481 0.163013 -0.906551 -0.102474
  83. qux four -1.083530 0.097077 0.224977 0.251739
  84. four 0.943804 1.356789 -0.953357 0.592986
Add Comment
Please, Sign In to add comment