pandas中df.ix, df.loc, df.iloc 的使用场景以及区别: https://stackoverflow.com/questions/31593201/pandas-iloc-vs-ix-vs-loc-explanation # Note: in pandas version 0.20.0 and above, ix is deprecated and the use of loc and iloc is encouraged instead. # First, a reca
Pandas--ix vs loc vs iloc区别 0. DataFrame DataFrame 的构造主要依赖如下三个参数: data:表格数据: index:行索引: columns:列名: index 对行进行索引,columns 对列进行索引: import pandas as pd data = [[1,2,3],[4,5,6]] index = [0,1] columns=['a','b','c'] df = pd.DataFrame(data=data, index=index
In [2]: df Out[2]: A B 0 1.068932 -0.794307 2 -0.470056 1.192211 4 -0.284561 0.756029 6 1.037563 -0.267820 8 -0.538478 -0.800654 In [5]: df.iloc[[2]] Out[5]: A B 4 -0.284561 0.756029 In [6]: df.loc[[2]] Out[6]: A B 2 -0.470056 1.192211 一个是按照index的序值.
使用pandas创建一个对象 In [1]: import pandas as pd In [2]: import numpy as np In [3]: df = pd.DataFrame(np.random.randn(6,4),index=pd.date_range(',periods=6),columns=list('ABCD')) In [4]: df Out[4]: A B C D 2018-01-01 -0.603510 0.269480 0.197354 -0.433003 20
先看代码: In [46]: import pandas as pd In [47]: data = [[1,2,3],[4,5,6]] In [48]: index = [0,1] In [49]: columns=['a','b','c'] In [50]: df = pd.DataFrame(data=data, index=index, columns=columns) In [51]: df Out[51]: a b c 0 1 2 3 1 4 5 6 1. loc--通过行标签索引行
df.at 一次只能访问一个值. df.loc能够选取多行多列. In [25]: %timeit df.loc[('a', 'A'), ('c', 'C')] 10000 loops, best of 3: 187 µs per loop In [26]: %timeit df.at[('a', 'A'), ('c', 'C')] 100000 loops, best of 3: 8.33 µs per loop at修改值的速度大概是loc的十倍. 来源: https://stackover
Different Choices for Indexing 1. loc--通过行标签索引行数据 1.1 loc[1]表示索引的是第1行(index 是整数) import pandas as pd data = [[1,2,3],[4,5,6]] index = [0,1] columns=['a','b','c'] df = pd.DataFrame(data=data, index=index, columns=columns) print df.loc[1] ''' a 4 b 5 c