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