Advertisement
elena1234

inspect the index and multiple indexes in Python

Mar 3rd, 2023 (edited)
1,001
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.60 KB | Source Code | 0 0
  1. index = cars.index
  2.  
  3. index.has_duplicates # return True or False
  4.  
  5. index.value_counts() # can see the most frequent value
  6.  
  7. #########################################
  8. titanic.loc[1] # only by first index
  9. titanic.loc[(slice(None), slice('female')), :] # only by second index
  10. titanic.loc[(slice(1), slice('female')), :]
  11. titanic.loc[(1,'female'), 'age')]
  12. titanic.loc[([1,2],'female'), 'age')]
  13.  
  14. titanic.swaplevel().sort_index() # swap the indexes and sort
  15.  
  16. titanic.reset_index()
  17.  
  18. #########################################
  19. cars.set_index(["model_year", "origin"]).sort_index(ascending = [True, True])
  20.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement