Pandas中ix,loc,iloc有什么区别?
2017-01-10 23:34
681 查看
直接看例子:
解析:
loc 在index的标签上进行索引,范围包括start和end.
iloc 在index的位置上进行索引,不包括end.
ix 先在index的标签上索引,索引不到就在index的位置上索引(如果index非全整数),不包括end.
建议: 为了避免歧义,建议优先选择loc和iloc
>>> data = pd.Series(np.arange(10), index=[49,48,47,46,45, 1, 2, 3, 4, 5]) >>> data 49 0 48 1 47 2 46 3 45 4 1 5 2 6 3 7 4 8 5 9 dtype: int64 >>> data.iloc[:3] 49 0 48 1 47 2 dtype: int64 >>> data.loc[:3] 49 0 48 1 47 2 46 3 45 4 1 5 2 6 3 7 dtype: int64 >>> data.ix[:3] 49 0 48 1 47 2 46 3 45 4 1 5 2 6 3 7 dtype: int64
解析:
loc 在index的标签上进行索引,范围包括start和end.
iloc 在index的位置上进行索引,不包括end.
ix 先在index的标签上索引,索引不到就在index的位置上索引(如果index非全整数),不包括end.
>>> data.iloc[:6] 49 0 48 1 47 2 46 3 45 4 1 5 dtype: int64 >>> data.loc[:6] KeyError: 6 >>> data.ix[:6] #因为index里面不包含标签6,index都是整数 KeyError: 6 >>> data= pd.Series(np.arange(10), index=['a','b','c','d','e', 1, 2, 3, 4, 5]) >>> data a 0 b 1 c 2 d 3 e 4 1 5 2 6 3 7 4 8 5 9 dtype: int64 >>> data.ix[:6] a 0 b 1 c 2 d 3 e 4 1 5 dtype: int64 >>> data.loc[:6] TypeError: cannot do slice indexing
建议: 为了避免歧义,建议优先选择loc和iloc
相关文章推荐
- Pandas中ix,loc,iloc有什么区别?
- Pandas中ix,loc,iloc有什么区别?
- Pandas——ix vs loc vs iloc区别
- python pandas中ix,iloc,loc的区别
- python pandas (ix & iloc &loc) 的区别
- python pandas (ix & iloc &loc) 的区别
- pandas中df.ix, df.loc, df.iloc 的使用场景以及区别
- Pandas中loc,iloc,ix的区别
- python pandas (ix & iloc &loc) 的区别
- python pandas (ix & iloc &loc) 的区别
- python pandas (ix & iloc &loc) 的区别
- python pandas (ix & iloc &loc) 的区别
- Pandas——ix vs loc vs iloc
- pandas数据索引:loc、iloc和ix
- Pandas的 loc iloc ix 区别
- Pandas的 loc iloc ix 区别
- python pandas中的ix,loc和iloc
- pandas中Dataframe的查询方法([], loc, iloc, at, iat, ix)
- Pandas中iloc和loc以及ix的区别
- python pandas.DataFrame选取、修改数据最好用.loc,.iloc,.ix