python pandas (ix & iloc &loc) 的区别
2018-02-19 12:36
746 查看
loc——通过行标签索引行数据
iloc——通过行号索引行数据
ix——通过行标签或者行号索引行数据(基于loc和iloc 的混合)
同理,索引列数据也是如此!举例说明:
1、分别使用loc、iloc、ix 索引第一行的数据:
(1)loc
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
(2)iloc
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
(3)ix
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
2、分别使用loc、iloc、ix 索引第一列的数据:
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
3、分别使用loc、iloc、ix 索引多行的数据:
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
4、分别使用loc、iloc、ix 索引多列的数据:
iloc——通过行号索引行数据
ix——通过行标签或者行号索引行数据(基于loc和iloc 的混合)
同理,索引列数据也是如此!举例说明:
1、分别使用loc、iloc、ix 索引第一行的数据:
(1)loc
import pandas as pd data=[[1,2,3],[4,5,6]] index=['a','b']#行号 columns=['c','d','e']#列号 df=pd.DataFrame(data,index=index,columns=columns)#生成一个数据框 #print df.loc['a'] ''' c 1 d 2 e 3 ''' print df.loc[0] #这个就会出现错误 ''' TypeError: cannot do label indexing on <class 'pandas.indexes.base.Index'> with these indexers [1] of <type 'int'> '''1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
(2)iloc
import pandas as pd data=[[1,2,3],[4,5,6]] index=['a','b']#行号 columns=['c','d','e']#列号 df=pd.DataFrame(data,index=index,columns=columns)#生成一个数据框 print df.iloc[0] ''' c 1 d 2 e 3 ''' print df.iloc['a'] ''' TypeError: cannot do positional indexing on <class 'pandas.indexes.base.Index'> with these indexers [a] of <type 'str'> '''1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
(3)ix
import pandas as pd data=[[1,2,3],[4,5,6]] index=['a','b']#行号 columns=['c','d','e']#列号 df=pd.DataFrame(data,index=index,columns=columns)#生成一个数据框 print df.ix[0] ''' c 1 d 2 e 3 ''' print df.ix['a'] ''' c 1 d 2 e 3 '''1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
2、分别使用loc、iloc、ix 索引第一列的数据:
import pandas as pd data=[[1,2,3],[4,5,6]] index=['a','b']#行号 columns=['c','d','e']#列号 df=pd.DataFrame(data,index=index,columns=columns)#生成一个数据框 print df.loc[:,['c']] print df.iloc[:,[0]] print df.ix[:,['c']] print df.ix[:,[0]] #结果都为 ''' c a 1 b 4 '''1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
3、分别使用loc、iloc、ix 索引多行的数据:
import pandas as pd data=[[1,2,3],[4,5,6]] index=['a','b']#行号 columns=['c','d','e']#列号 df=pd.DataFrame(data,index=index,columns=columns)#生成一个数据框 print df.loc['a':'b'] print df.iloc[0:1] print df.ix['a':'b'] print df.ix[0:1] #结果都为 ''' c d e a 1 2 3 b 4 5 6 '''1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
4、分别使用loc、iloc、ix 索引多列的数据:
import pandas as pd data=[[1,2,3],[4,5,6]] index=['a','b']#行号 columns=['c','d','e']#列号 df=pd.DataFrame(data,index=index,columns=columns)#生成一个数据框 print df.loc[:,'c':'d'] print df.iloc[:,0:2] print df.ix[:,'c':'d'] print df.ix[:,0:2] #结果都为 ''' c d a 1 2 b 4 5 '''转载于:http://blog.csdn.net/hecongqing/article/details/61927615
相关文章推荐
- 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) 的区别
- python pandas中ix,iloc,loc的区别
- python pandas.DataFrame选取、修改数据最好用.loc,.iloc,.ix
- Pandas中loc,iloc,ix的区别
- 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
- pandas中df.ix, df.loc, df.iloc 的使用场景以及区别
- python pandas.DataFrame选取、修改数据最好用.loc,.iloc,.ix
- python pandas.DataFrame选取、修改数据最好用.loc,.iloc,.ix
- Pandas中ix,loc,iloc有什么区别?
- python pandas中的ix,loc和iloc
- python pandas.DataFrame选取、修改数据最好用.loc,.iloc,.ix
- Pandas中ix,loc,iloc有什么区别?
- Pandas——ix vs loc vs iloc区别