python pandas中的ix,loc和iloc
来源:互联网 发布:nginx root index 编辑:程序博客网 时间:2024/06/06 09:36
环境: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
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,也可能不是,具有不确定性)
0 0
- python pandas中的ix,loc和iloc
- python pandas (ix & iloc &loc) 的区别
- python pandas中ix,iloc,loc的区别
- python pandas (ix & iloc &loc) 的区别
- python pandas (ix & iloc &loc) 的区别
- python pandas (ix & iloc &loc) 的区别
- Pandas中iloc和loc以及ix的区别
- Pandas中loc,iloc和ix的区别
- Pandas的 loc iloc ix 区别
- pandas中loc、iloc、ix的区别
- pandas中loc iloc ix的区别
- Pandas的 loc iloc ix 区别
- pandas中ix loc iloc的区别
- Pandas的 loc iloc ix 区别
- Pandas的 loc iloc ix 区别
- Pandas中loc,iloc,ix的区别
- python pandas.DataFrame选取、修改数据最好用.loc,.iloc,.ix
- python pandas.DataFrame选取、修改数据最好用.loc,.iloc,.ix
- 【SSLGZ 1763】观光旅游
- [UIApplication sharedApplication].keyWindow与[UIApplication sharedApplication].delegate.window];;
- 第五章 DIV
- linux常用命令-用户及权限管理
- 运算符
- python pandas中的ix,loc和iloc
- 6.1 Swift闭包的概念和分类
- StringBuffer
- jquery选择器eq,gt,lt英文
- Java 容器Collection(5)
- 读《从Paxos到Zookeeper 分布式一致性原理与实践》笔记之通信协议
- 三种技术实现PC1、PC2与PC3都通,而PC1与PC2不通
- JpGraph Error: 25049 JpGraph字体问题 的解决办法
- git统计的常用命令总结