loc是指location的意思,iloc中的i是指integer。这两者的原意是标签定位,区别如下:
loc works on labels in the index.
iloc works on the positions in the index (so it only takes integers).
也就是说,loc是能够工作在自定义的index标签,而iloc是工作在默认的index标签数字
原文:https://blog.youkuaiyun.com/dark_tone/article/details/80174335
如上table定义了一个index,那么loc就根据这个index来索引对应的行。
iloc是根据行号来索引,行号从0开始,逐次加1。
两个区别
1,.ioc以用户自定义的行标签为参考,.iloc依然使用系统默认。
2,loc[]是包括了]最右边,而iloc跟python设定相同,不包括]最右边。
例如:
print(table.iloc[0])
print(table.loc['a'])
一:
loc:通过行标签索引数据
iloc:通过行号索引行数据
ix:通过行标签或行号索引数据(基于loc和iloc的混合)
如果说loc是按照索引(index)的值来选取的话,那么iloc就是按照索引的位置来进行选取。iloc不关心索引的具体值是多少,只关心位置是多少,所以使用iloc时方括号中只能使用数值
二:
当要单独获取多行数据的时。
print(table.iloc[[0,6]])
print(table.loc[['a','b']])
当要进行切片获取多行数据时。
print(table[0:6]) --dateframe直接切片,可获取多行数据
print(table.iloc[0:6])
loc与第一种方法不同之处在于会把第6行也选择进去,而第一种方法只会选择到第5行为止
print(table.loc['a':'b'])
#####总结:获取一列可以直接用df[],获取一行要用df.loc[],获取多行多列要用两个[[,]]形式,如果要通过行和列来精确某一个位置的数据,可以使用df[列索引][行索引],并且不能把顺序搞反。
三, 可见传统df[0:3]与df.iloc[0:3]相同
df[] 中括号里有一个值或一个列表时,取的是列数据。
当中括号里是一个切片时,是指行数据的切片。
如果切出城市和环比两个列,loc第一个参数说明取所有行
如果想要连续切片城市,定基,环比 3个列,因为自定义了行标签,无法使用loc数字切片,可以用.iloc,或者老老实实的写完3个行标签。
df.loc[:,1:3] #错误写法,行标签已经自定义了
df.iloc[:,1:4] #正确写法
df.loc[:,['城市':'环比']] #错误写法
df.loc[:,'城市':'环比'] #正确写法,切片不加[]符号
df.loc[:,['城市','定基','环比']] #正确写法,多个标签成为列表,要加[]
df[['城市','定基','环比']] #正确写法
总结:对任意行列切片都可以使用.loc和.iloc,但是要注意写法细节。.iloc缺点是要去数行列,纯数字也不直观;.loc使用用户自定义标签,非常直观,自己以后用这个,要注意包涵关系和切片不加[]符号。
原文:https://blog.youkuaiyun.com/dark_tone/article/details/80174335
四:AT。
at的使用方法与loc类似,但是比loc有更快的访问数据的速度,而且只能访问单个元素,不能访问多个元素。
In [20]: timeit data_fecha.at[fecha_1,'rnd_1']
The slowest run took 3783.11 times longer than the fastest. This could mean that an intermediate result is being cached.
100000 loops, best of 3: 11.3 µs per loop
In [21]: timeit data_fecha.loc[fecha_1,'rnd_1']
The slowest run took 121.24 times longer than the fastest. This could mean that an intermediate result is being cached.
10000 loops, best of 3: 192 µs per loop
In [22]: data_fecha.at[fecha_1,'rnd_1']
Out[22]: 17
五,iat。
iat对于iloc的关系就像at对于loc的关系,是一种更快的基于索引位置的选择方法,同at一样只能访问单个元素。
In [23]: data_fecha.iat[1,0]
Out[23]: 1
In [24]: timeit data_fecha.iat[1,0]
The slowest run took 6.23 times longer than the fastest. This could mean that an intermediate result is being cached.
100000 loops, best of 3: 8.77 µs per loop
In [25]: timeit data_fecha.iloc[1,0]
10000 loops, best of 3: 158 µs per loop
六,ix
以上说过的几种方法都要求查询的秩在索引中,或者位置不超过长度范围,而ix允许你得到不在DataFrame索引中的数据。
In [28]: date_1 = dt.datetime(2013, 1, 10, 8, 30)
...: date_2 = dt.datetime(2013, 1, 13, 4, 20)
...:
...: # 生成切片数据
...: data_fecha.ix[date_1: date_2]
Out[28]:
rnd_1 rnd_2 rnd_3
fecha
2013-01-11 19 17 19
2013-01-12 10 9 17
2013-01-13 15 3 10
原文:https://blog.youkuaiyun.com/wr339988/article/details/65446138/
七,通过条件获取行数据及行号的方法。
一、根据列条件,获取行索引号并转成列表
#在dataframe中根据一定的条件,得到符合要求的某些行元素所在的位置
import pandas as pd
df = pd.DataFrame({'BoolCol': [1, 2, 3, 3, 4],'attr': [22, 33, 22, 44, 66]},
index=[10,20,30,40,50])
print(df)
a = df[(df.BoolCol==3)&(df.attr==22)].index.tolist()
print(a)
输出
BoolCol attr
10 1 22
20 2 33
30 3 22
40 3 44
50 4 66
[30]
注意:
df[(df.BoolCol3)&(df.attr22)].index返回的是index对象列表,需转换为普通列表格式时用tolist()方法
二、根据列条件,选取dataframe数据框中的数据,得到一个新的dateframe(也可通过使用
.index.tolist()方法得到行索引列表)。
# 选取等于某些值的行记录 用 ==
df.loc[df['column_name'] == some_value]
# 选取某列是否是某一类型的数值 用 isin,参数some_values是一个列表,里面放元素
df.loc[df['column_name'].isin(some_values)]
# 多种条件的选取 用 &
df.loc[(df['column'] == some_value) & df['other_column'].isin(some_values)]
# 选取不等于某些值的行记录 用 !=
df.loc[df['column_name'] != some_value]
# isin返回一系列的数值,如果要选择不符合这个条件的数值使用~
df.loc[~df['column_name'].isin(some_values)]
注:
df.loc[df[“BoolCol”] == 3] 与 df[(df.BoolCol3)] 等价
df[‘BoolCol’].isin([3]) 与 df.BoolCol3 等价
df[‘BoolCol’] 与 df.BoolCol 等价
参考:https://blog.youkuaiyun.com/huang_susan/article/details/80626698