Pandas中loc,iloc与直接切片的区别
最近使用pandas,一直搞不清楚其中几种切片方法的区别,今天专门看了一下。
0. 把Series的行index或Dataframe的列名直接当做属性来索引。
如:
s.index_name df.column_name
但是这种方法索引的名字可能会与自带的方法冲突,比如min,max等等,所以可能会失效。另外,在新版本中,这种索引方法不能作为左值。
1. df[]直接索引
- 直接索引索引的是列,方口号里面的内容一般是列索引名。也可以接受一个列名组成的list来接受多个列名。
df['A']
df[['A', 'B']]
如果要交换两列,直接用这样的方法是不对的:
df.loc[:,['B', 'A']] = df[['A', 'B']]
这是因为pandas默认在赋值的时候回匹配列名,这里面的AB和BA实际上没有区别。如果想要交换两列的话,应该使用AB两列的值作为右值,这样就不带列索引名了。
df.loc[:,['B', 'A']] = df[['A', 'B']].values
- 索引slice对象,索引的是行,因为这样做更加符合常理
df=pd.DataFrame(np.arange(16).reshape((4,4)),index=list(range(4)),columns=['a','b','c','d']) df
Out[4]:
a b c d
0 0 1 2 3
1 4 5 6 7
2 8 9 10 11
3 12 13 14 15
df[0:1]
Out[6]:
a b c d
0 0 1 2 3
这里如果是Series,就可以使用单独的数字来索引;如果是Dataframe,就不可以,需要使用Python的slice对象形式的来索引。
2. loc,基于label的索引
由于pandas处理的是具有标签的表格对象,所以设计一套基于标签的索引方法是必要的,这就是loc
loc索引要注意的是:
- loc是基于标签的,如果给出的索引中包含没有的标签,会报错
- loc索引的开闭区间机制和Python传统的不同,而是与MATLAB类似的双侧闭区间,即只要出现,就会包含该标签
- 数字也可以放入loc中当做索引,但是这时数字指的是标签而不是 位置
df.loc[0:3,'a':'c']
Out[9]:
a b c
0 0 1 2
1 4 5 6
2 8 9 10
3 12 13 14
注意上面的0:3实际上并不是数字,而是行的label。
loc也可以接受布尔索引来取出其中的一些行
df.loc[df.loc[:,'a']>1]
3. iloc,完全基于位置的索引
iloc的用法完全和numpy中的数字索引一样,开闭区间的逻辑也和Python是相同的。
要注意的是,如果iloc方括号中直接给定一个数字或者一个slice的话,默认索引的是行。其中数字的情况会返回一个Series
df.loc[:,df.loc[1]>1]
Out[21]:
a b c d
0 0 1 2 3
1 4 5 6 7
2 8 9 10 11
3 12 13 14 15 df.loc[:,df.loc[1]>4]
Out[22]:
b c d
0 1 2 3
1 5 6 7
2 9 10 11
3 13 14 15 df.iloc[1]
Out[23]:
a 4
b 5
c 6
d 7
Name: 1, dtype: int32 type(df.iloc[1])
Out[24]: pandas.core.series.Series
Pandas中loc,iloc与直接切片的区别的更多相关文章
- python pandas 中 loc & iloc 用法区别
转自:https://blog.csdn.net/qq_21840201/article/details/80725433 ### 随机生DataFrame 类型数据import pandas as ...
- Pandas:loc iloc ix用法
参考:Pandas中关于 loc \ iloc \ ix 用法的理解 相同点 使用形式都是 df.xxx[ para1 , para2 ] #xxx表示loc iloc ix#df表示一个DataFr ...
- Pandas中Loc用法总结
摘自:http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.loc.html 具体用法,假设数据源为: > ...
- [译]pandas中的iloc loc的区别?
loc 从特定的 gets rows (or columns) with particular labels from the index. iloc gets rows (or columns) a ...
- pandas中Loc vs. iloc vs. ix vs. at vs. iat?
loc: only work on indexiloc: work on positionix: You can get data from dataframe without it being in ...
- Pandas的 loc iloc ix 区别
先看代码: In [46]: import pandas as pd In [47]: data = [[1,2,3],[4,5,6]] In [48]: index = [0,1] In [49]: ...
- pandas 定位 loc,iloc,ix
In [114]: df Out[114]: A B C D 2018-06-30 0.318501 0.613145 0.485612 0.918663 2018-07-31 0.614796 0. ...
- pandas的loc, iloc, ix的操作
参考: https://blog.csdn.net/xw_classmate/article/details/51333646 1. loc——通过行标签索引行数据 2. iloc——通过行号获取行数 ...
- Pandas之loc\iloc\ix
---------------------------------------------------------------------------------------------------- ...
随机推荐
- 自定义的cell上面有图片时,如果产生了重用,图片可能会错乱问题
当被重用的cell将要显示时,会调用这个方法,这个方法最大的用武之地是当你自定义的cell上面有图片时,如果产生了重用,图片可能会错乱(当图片来自异步下载时及其明显),这时我们可以重写这个方法把内容抹 ...
- html Css PC 移动端 公用部分样式代码整理
css常用公用部分样式代码整理: body, html, div, blockquote, img, label, p, h1, h2, h3, h4, h5, h6, pre, ul, ol, li ...
- js操作表格
js 操作table: insertRow(),deleteRow(),insertCell(),deleteCell()方法 表格有几行: var trCnt = table.rows.length ...
- JAVA常用知识总结(十四)——Servlet
Servlet属于线程安全的吗? Servlet不是线程安全的! 谈谈转发和重定向的区别 请求转发: request.getRequestDispatcher("/king_l2lu.jsp ...
- Spring源码:Spring IoC容器加载过程(2)
Spring源码版本:4.3.23.RELEASE 一.加载XML配置 通过XML配置创建Spring,创建入口是使用org.springframework.context.support.Class ...
- [问题记录]Java关于可变参数重载问题的测试
突然发现java的可变参数重载是存在歧义的,只是不知道java内部是怎么处理的,特意做如下实验: 测试代码如下: /** * Created by ieayoio on 16-3-1. */ publ ...
- log4j日志工具类
/** * log4j日志Util */public class LogUtil { private static Logger logger=null; static{ StackTraceElem ...
- CF1149A Prefix Sum Primes
思路: 质数一定是奇数.实现: #include <bits/stdc++.h> using namespace std; int main() { int n, t, x, y; whi ...
- leetcode378 Kth Smallest Element in a Sorted Matrix
思路1: 使用堆. 实现: class Solution { public: int kthSmallest(vector<vector<int>>& matrix, ...
- Python安装第三方库文件工具——pip
Python安装第三方库文件一般使用pip. 1.pip的安装 (1)下载pip 进入https://pypi.python.org/pypi/pip#downloads