Pandas-ix vs loc vs iloc differences

Keywords: Attribute

  1. loc -- Indexing row data through row labels

1.1 loc[1] represents the first row of the index (index is an integer)

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    6 

1.2 loc ['d'] indicates that the index is line'd'(index is a character)

import pandas as pd  
data = [[1,2,3],[4,5,6]]  
index = ['d','e']  
columns=['a','b','c']  
df = pd.DataFrame(data=data, index=index, columns=columns)  
print df.loc['d']  
a    1 
b    2 
c    3 

1.3 If you want to index column data, doing so will cause an error

import pandas as pd  
data = [[1,2,3],[4,5,6]]  
index = ['d','e']  
columns=['a','b','c']  
df = pd.DataFrame(data=data, index=index, columns=columns)  
print df.loc['a']  
KeyError: 'the label [a] is not in the [index]' 

1.4 loc can obtain multiple rows of data

import pandas as pd  
data = [[1,2,3],[4,5,6]]  
index = ['d','e']  
columns=['a','b','c']  
df = pd.DataFrame(data=data, index=index, columns=columns)  
print df.loc['d':]  
   a  b  c 
d  1  2  3 
e  4  5  6 

1.5 loc Extension - Index a row, a column

import pandas as pd  
data = [[1,2,3],[4,5,6]]  
index = ['d','e']  
columns=['a','b','c']  
df = pd.DataFrame(data=data, index=index, columns=columns)  
print df.loc['d',['b','c']]  
b    2 
c    3 

1.6 loc Extension - Index a column

import pandas as pd  
data = [[1,2,3],[4,5,6]]  
index = ['d','e']  
columns=['a','b','c']  
df = pd.DataFrame(data=data, index=index, columns=columns)  
print df.loc[:,['c']]  
   c 
d  3 
e  6 

Of course, the most direct way to get a column of data is df. [column label], but when the column label is unknown, column data can be obtained in this way.

It should be noted that the index of dataframe [1:3] contains 1,2,3, which is different from the usual index.

  1. iloc - Obtaining row data by line number

2.1 Enter the line number if you want to get which line

import pandas as pd  
data = [[1,2,3],[4,5,6]]  
index = ['d','e']  
columns=['a','b','c']  
df = pd.DataFrame(data=data, index=index, columns=columns)  
print df.loc[1]  
a    4 
b    5 
c    6 

2.2 Errors will be reported through the row label index

import pandas as pd  
data = [[1,2,3],[4,5,6]]  
index = ['d','e']  
columns=['a','b','c']  
df = pd.DataFrame(data=data, index=index, columns=columns)  
print df.iloc['a']  
TypeError: cannot do label indexing on <class 'pandas.core.index.Index'> with these indexers [a] of <type 'str'> 

2.3 Multiple rows can also be indexed by line number

import pandas as pd  
data = [[1,2,3],[4,5,6]]  
index = ['d','e']  
columns=['a','b','c']  
df = pd.DataFrame(data=data, index=index, columns=columns)  
print df.iloc[0:]  
   a  b  c 
d  1  2  3 
e  4  5  6 

2.4 iloc index column data

import pandas as pd  
data = [[1,2,3],[4,5,6]]  
index = ['d','e']  
columns=['a','b','c']  
df = pd.DataFrame(data=data, index=index, columns=columns)  
print df.iloc[:,[1]]  
   b 
d  2 
e  5 
  1. ix -- Combining the first two hybrid indexes

3.1 Through Line Number Index

import pandas as pd  
data = [[1,2,3],[4,5,6]]  
index = ['d','e']  
columns=['a','b','c']  
df = pd.DataFrame(data=data, index=index, columns=columns)  
print df.ix[1]  
a    4 
b    5 
c    6 

3.2 Indexed by row labels

import pandas as pd  
data = [[1,2,3],[4,5,6]]  
index = ['d','e']  
columns=['a','b','c']  
df = pd.DataFrame(data=data, index=index, columns=columns)  
print df.ix['e']  
a    4 
b    5 
c    6 

http://pandas.pydata.org/pandas-docs/stable/indexing.html#different-choices-for-indexing

Posted by Renlok on Tue, 05 Feb 2019 02:57:16 -0800