数据切片
import pandas as pd
import numpy as np
date1 = pd.date_range("20170813",periods=6)
df = pd.DataFrame(data=np.random.randint(3,9,size=(6,6)),index=date1,columns=list(["a","b","c","d","e","f"]))
print(df)
a b c d e f
2017-08-13 6 5 5 5 4 4
2017-08-14 3 3 4 3 4 8
2017-08-15 3 5 5 6 5 4
2017-08-16 5 3 5 3 3 4
2017-08-17 3 6 3 4 8 6
2017-08-18 7 6 5 7 4 6
print(df["a"])
2017-08-13 6
2017-08-14 3
2017-08-15 3
2017-08-16 5
2017-08-17 3
2017-08-18 7
Freq: D, Name: a, dtype: int32
print(df[0:1])
a b c d e f
2017-08-13 5 6 4 3 3 7
print(df.loc["2017-08-18",:])
print(df.loc[:,("a","b","f")])
print(df.loc["2017-08-14",["b","a","e"]])
a 7
b 6
c 5
d 7
e 4
f 6
Name: 2017-08-18 00:00:00, dtype: int32
a b f
2017-08-13 5 6 7
2017-08-14 5 4 7
2017-08-15 3 6 4
2017-08-16 7 7 7
2017-08-17 7 5 7
2017-08-18 7 8 4
b 4
a 5
e 6
Name: 2017-08-14 00:00:00, dtype: int32
print(df.iloc[3,3])
print(df.iloc[3:5,3:5])
print(df.iloc[[1,2,5],[2,4,5]])
3
d e
2017-08-16 3 5
2017-08-17 4 6
c e f
2017-08-14 8 6 7
2017-08-15 6 5 4
2017-08-18 5 3 4
print(df.ix[[0,2],[0,3]])
print(df.ix[[0,2],["a","e"]])
a d
2017-08-13 5 3
2017-08-15 3 5
a e
2017-08-13 5 3
2017-08-15 3 5
print(df[[True,True,False,True,True,False]])
a b c d e f
2017-08-13 6 5 5 5 4 4
2017-08-14 3 3 4 3 4 8
2017-08-16 5 3 5 3 3 4
2017-08-17 3 6 3 4 8 6
df.loc[df["a"]>5,df.loc["2017-08-15",:]>4]
b c d e
2017-08-13 5 5 5 4
2017-08-18 6 5 7 4