11_pandas_intro.py 1.1 KB

123456789101112131415161718192021222324252627282930313233
  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. s = pd.Series([1,3,6,np.nan,4,1]) # similar with 1D numpy
  11. print(s)
  12. dates = pd.date_range('20160101', periods=6)
  13. df = pd.DataFrame(np.random.randn(6,4), index=dates, columns=['A','B','C','D'])
  14. print(df['B'])
  15. df2 = pd.DataFrame({'A' : 1.,
  16. 'B' : pd.Timestamp('20130102'),
  17. 'C' : pd.Series(1,index=list(range(4)),dtype='float32'),
  18. 'D' : np.array([3] * 4,dtype='int32'),
  19. 'E' : pd.Categorical(["test","train","test","train"]),
  20. 'F' : 'foo'})
  21. print(df2)
  22. print(df2.dtypes)
  23. print(df.index)
  24. print(df.columns)
  25. print(df.values)
  26. print(df.describe())
  27. print(df.T)
  28. print(df.sort_index(axis=1, ascending=False))
  29. print(df.sort_values(by='B'))