importpandas as pdimportnumpy as np
df= pd.DataFrame(np.arange(24).reshape(6,4), columns=list('ABCD'), index=list('abcdef'))print(df)#直接切片定位法 总结:取行时只能用切片,取列时只能用list选择列名#切片行时#注意切片用真实索引时是左闭右闭,
print(df['a':'d'])#用索引序号时是左闭右开
print(df[0:3])#当真实索引和索引序号相同时,切片是左闭右开
print(df.reset_index())print(df.reset_index()[0:3])#切片列时#列索引必须是用真实索引,不能用索引序号,否则报错
print(df['a':'d'][['C','D']])print(df['a':'d'])#且此方法不能对列名做切片,会报错#print(df[['B':'D']])#print(df[0:3][[2:3]])
#loc()函数定位法#loc()函数的固定结构如下,先写行索引,再写列索引#注意loc()的切片是左闭右闭,iloc()的切片是左闭右开
print(df.loc['b':'e',['A','D']])print(df.loc[ df['A']> 8])#会报错,loc()只接受真实索引,iloc()只接受索引的序号,即必须是整数#print(df.loc[ 2:4, ['A','D']])
#iloc()函数定位法#iloc()只接受索引的序号,切片是左闭右开,同直接[]切片定位方法用索引序号时是一样的
print(df.iloc[0:4, 1:3])#at()函数#只能定位单个元素,无法定位多个,用法同loc()
print(df.at['a','C'])#iat()函数#只能定位单个元素,无法定位多个,用法同iloc()
print(df.iat[0,2])#ix()#当索引列是时间时,允许用未在索引列中出现的时间做切片#在pandas版本0.20.0及其以后版本中,ix已经不被推荐使用,建议采用iloc和loc实现ix。
importrandomimportdatetime as dt
rnd_1= [random.randrange(1,20) for x in range(1000)]
rnd_2= [random.randrange(1,20) for x in range(1000)]
rnd_3= [random.randrange(1,20) for x in range(1000)]
fecha= pd.date_range('2012-4-10', '2015-1-4')
data= pd.DataFrame({'fecha':fecha, 'rnd_1': rnd_1, 'rnd_2': rnd_2, 'rnd_3': rnd_3})print(data.describe())
date_1= dt.datetime(2013, 1, 10, 8, 30)
date_2= dt.datetime(2013, 1, 13, 4, 20)
data_fecha= data.set_index('fecha')print(data_fecha.ix[date_1: date_2])#print(data_fecha.loc[date_1,date_2])#参考:https://blog.csdn.net/wr339988/article/details/65446138
#A B C D#a 0 1 2 3#b 4 5 6 7#c 8 9 10 11#d 12 13 14 15#e 16 17 18 19#f 20 21 22 23#A B C D#a 0 1 2 3#b 4 5 6 7#c 8 9 10 11#d 12 13 14 15#A B C D#a 0 1 2 3#b 4 5 6 7#c 8 9 10 11#index A B C D#0 a 0 1 2 3#1 b 4 5 6 7#2 c 8 9 10 11#3 d 12 13 14 15#4 e 16 17 18 19#5 f 20 21 22 23#index A B C D#0 a 0 1 2 3#1 b 4 5 6 7#2 c 8 9 10 11#C D#a 2 3#b 6 7#c 10 11#d 14 15#A B C D#a 0 1 2 3#b 4 5 6 7#c 8 9 10 11#d 12 13 14 15#A D#b 4 7#c 8 11#d 12 15#e 16 19#A B C D#d 12 13 14 15#e 16 17 18 19#f 20 21 22 23#B C#a 1 2#b 5 6#c 9 10#d 13 14#2#2#rnd_1 rnd_2 rnd_3#count 1000.000000 1000.000000 1000.000000#mean 9.912000 10.140000 10.134000#std 5.370204 5.461156 5.458006#min 1.000000 1.000000 1.000000#25% 5.000000 5.000000 5.000000#50% 10.000000 10.000000 10.000000#75% 14.000000 15.000000 15.000000#max 19.000000 19.000000 19.000000#rnd_1 rnd_2 rnd_3#fecha#2013-01-11 6 14 9#2013-01-12 10 19 11#2013-01-13 14 4 14