Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2019
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.20 KB | None | 0 0
  1. import pandas as pd
  2. import numpy as np
  3. # Crear un multi index desde tuplas
  4. indices = [('Santiago', 2000), ('Santiago', 2010),
  5. ('California', 2000), ('California', 2010),
  6. ('New York', 2000), ('New York', 2010)]
  7. m_indice = pd.MultiIndex.from_tuples(indices)
  8. df = pd.DataFrame([33871648, 37253956, 18976457, 19378102, 20851820, 25145561],
  9. columns=['Poblacion'], index=m_indice)
  10. print(df)
  11. # Poblacion
  12. # Santiago 2000 33871648
  13. # 2010 37253956
  14. # California 2000 18976457
  15. # 2010 19378102
  16. # New York 2000 20851820
  17. # 2010 25145561
  18. # Seleccionar mas facil multiples grupos
  19. df.loc['Santiago']
  20. # Poblacion
  21. # 2000 33871648
  22. # 2010 37253956
  23. df.loc['Santiago', 2010]
  24. # Poblacion 37253956
  25. # Name: (Santiago, 2010), dtype: int64
  26. m_indice = pd.MultiIndex.from_tuples([('Categoria 1', 'Primer Valor'),
  27. ('Categoria 1', 'Segundo Valor'),
  28. ('Categoria 1', 'Tercer Valor'),
  29. ('Categoria 2', 'Primer Valor'),
  30. ('Categoria 2', 'Segundo Valor'),
  31. ('Categoria 2', 'Tercer Valor'),])
  32. df = pd.DataFrame(np.random.randint(1, high=5, size=(6,4)), columns=list('abcd'), index=m_indice)
  33. df
  34. # a b c d
  35. # Categoria 1 Primer Valor 4 1 3 3
  36. # Segundo Valor 3 3 4 1
  37. # Tercer Valor 4 4 4 2
  38. # Categoria 2 Primer Valor 4 4 4 3
  39. # Segundo Valor 4 3 4 3
  40. # Tercer Valor 2 1 4 3
  41.  
  42. df.loc['Categoria 1', 'a']
  43. # Primer Valor 4
  44. # Segundo Valor 3
  45. # Tercer Valor 4
  46. # Name: a, dtype: int64
  47.  
  48. df.loc[('Categoria 1', 'Tercer Valor'):('Categoria 2', 'Sengundo Valor')]
  49. # a b c d
  50. # Categoria 1 Tercer Valor 4 4 4 2
  51. # Categoria 2 Primer Valor 4 4 4 3
  52. # Segundo Valor 4 3 4 3
  53.  
  54. df.loc[('Categoria 1', 'Tercer Valor'): 'Categoria 2']
  55. # a b c d
  56. # Categoria 1 Tercer Valor 4 4 4 2
  57. # Categoria 2 Primer Valor 4 4 4 3
  58. # Segundo Valor 4 3 4 3
  59. # Tercer Valor 2 1 4 3
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement