python pandas中的ix,loc和iloc

环境: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,也可能不是,具有不确定性)

你可能感兴趣的:(python,pandas)