python pandas中的ix,loc和iloc
2016-12-17 10:44
731 查看
环境:python 3.4 + pandas 0.19.1
先看一下官方文档对这3个api的描述:
DataFrame.ix
A primarily label-location based indexer, with integer position fallback.【支持根据索引值,或者按顺序位置定位】
.ix[] supports mixed integer and label based access. It is primarily label based, but will fall back to integer positional access unless the corresponding axis is of integer type.【按顺序位置定位,要求索引不能是数字】
DataFrame.loc
Purely label-location based indexer for selection by label.【按索引值定位】
.loc[] is primarily label based, but may also be used with a boolean array.
DataFrame.iloc
Purely integer-location based indexing for selection by position.【按顺序位置定位】
.iloc[] is primarily integer position based (from 0 to length-1 of the axis), but may also be used with a boolean array.
然后在一个函数中需要对DF进行修改,使用了iloc,但是用的是交叉的方式(df.iloc[1, 'col1'],index使用位置,column使用了索引值),结果发现修改没有生效,报出了警告:
SettingWithCopyWarning:
A value is trying to be set on a copy of a slice from a DataFrame
See the the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
里面提到“When setting values in a pandas object, care must be taken to avoid what is called chained indexing. ”,具体详细内容参见链接,不细讲。简单说一下结论:当需要修改pandas的对象时(Series/DataFrame),以如下规则(对ix/loc/iloc一样):
应该使用的方法:df.loc[:,'one'](修改行/列),df.loc['a', 'one'](修改具体某个元素);
不应使用的方法:df['one'](修改行/列),df['a']['one']、df.loc['a']['one'](修改具体某个元素)。(因为这些方法可能返回的是copy,也可能不是,具有不确定性)
先看一下官方文档对这3个api的描述:
DataFrame.ix
A primarily label-location based indexer, with integer position fallback.【支持根据索引值,或者按顺序位置定位】
.ix[] supports mixed integer and label based access. It is primarily label based, but will fall back to integer positional access unless the corresponding axis is of integer type.【按顺序位置定位,要求索引不能是数字】
DataFrame.loc
Purely label-location based indexer for selection by label.【按索引值定位】
.loc[] is primarily label based, but may also be used with a boolean array.
DataFrame.iloc
Purely integer-location based indexing for selection by position.【按顺序位置定位】
.iloc[] is primarily integer position based (from 0 to length-1 of the axis), but may also be used with a boolean array.
然后在一个函数中需要对DF进行修改,使用了iloc,但是用的是交叉的方式(df.iloc[1, 'col1'],index使用位置,column使用了索引值),结果发现修改没有生效,报出了警告:
SettingWithCopyWarning:
A value is trying to be set on a copy of a slice from a DataFrame
See the the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
里面提到“When setting values in a pandas object, care must be taken to avoid what is called chained indexing. ”,具体详细内容参见链接,不细讲。简单说一下结论:当需要修改pandas的对象时(Series/DataFrame),以如下规则(对ix/loc/iloc一样):
应该使用的方法:df.loc[:,'one'](修改行/列),df.loc['a', 'one'](修改具体某个元素);
不应使用的方法:df['one'](修改行/列),df['a']['one']、df.loc['a']['one'](修改具体某个元素)。(因为这些方法可能返回的是copy,也可能不是,具有不确定性)
相关文章推荐
- python pandas (ix & iloc &loc) 的区别
- python pandas.DataFrame选取、修改数据最好用.loc,.iloc,.ix
- python pandas (ix & iloc &loc) 的区别
- python pandas (ix & iloc &loc) 的区别
- python pandas (ix & iloc &loc) 的区别
- python pandas (ix & iloc &loc) 的区别
- python pandas.DataFrame选取、修改数据最好用.loc,.iloc,.ix
- python pandas.DataFrame选取、修改数据最好用.loc,.iloc,.ix
- python pandas中ix,iloc,loc的区别
- python pandas.DataFrame选取、修改数据最好用.loc,.iloc,.ix实现
- Python pandas.DataFrame在用ix, loc, iloc 索引的时候就相当于拷贝copy了一个对象
- python pandas.DataFrame选取、修改数据最好用.loc,.iloc,.ix
- python的pandas包数据框单层索引操作核心方法loc,iloc,ix,query
- python pandas.DataFrame选取、修改数据最好用.loc,.iloc,.ix
- python pandas (ix & iloc &loc) 的区别
- 浅谈pandas中Dataframe的查询方法([], loc, iloc, at, iat, ix)
- Pandas中ix,loc,iloc有什么区别?
- pandas数据索引:loc、iloc和ix
- pandas中Dataframe的查询方法([], loc, iloc, at, iat, ix)
- 在pandas中, DataFrame的 ix(loc, iloc)属性的设置误区,以及reindex可以找到两个df不同之处