12_selection.py 922 B

12345678910111213141516171819202122232425262728293031323334
  1. # View more python tutorials on my Youtube and Youku channel!!!
  2. # Youtube video tutorial: https://www.youtube.com/channel/UCdyjiB5H8Pu7aDTNVXTTpcg
  3. # Youku video tutorial: http://i.youku.com/pythontutorial
  4. """
  5. Please note, this code is only for python 3+. If you are using python 2+, please modify the code accordingly.
  6. """
  7. from __future__ import print_function
  8. import pandas as pd
  9. import numpy as np
  10. dates = pd.date_range('20130101', periods=6)
  11. df = pd.DataFrame(np.random.randn(6,4), index=dates, columns=['A', 'B', 'C', 'D'])
  12. print(df['A'], df.A)
  13. print(df[0:3], df['20130102':'20130104'])
  14. # select by label: loc
  15. print(df.loc['20130102'])
  16. print(df.loc[:,['A','B']])
  17. print(df.loc['20130102', ['A','B']])
  18. # select by position: iloc
  19. print(df.iloc[3])
  20. print(df.iloc[3, 1])
  21. print(df.iloc[3:5,0:2])
  22. print(df.iloc[[1,2,4],[0,2]])
  23. # mixed selection: ix
  24. print(df.ix[:3, ['A', 'C']])
  25. # Boolean indexing
  26. print(df[df.A > 0])