In the last blog post <https://blog.csdn.net/anshuai_aw1/article/details/82802769>
in , We've explained it in detail iloc and loc, I just mentioned it briefly ix. This is because compared to the previous 2 person ,ix More complicated , It's even more confusing .

therefore , This blog tries to describe it clearly through examples ix, Especially with iloc and loc The connection of .

catalog

1 use ix segmentation Series
<https://blog.csdn.net/anshuai_aw1/article/details/82801435#1%20%E4%BD%BF%E7%94%A8ix%E5%88%87%E5%88%86Series>

1.1 characteristic 1 give an example
<https://blog.csdn.net/anshuai_aw1/article/details/82801435#1.1%20%E7%89%B9%E7%82%B91%E4%B8%BE%E4%BE%8B>

1.2 characteristic 2 give an example
<https://blog.csdn.net/anshuai_aw1/article/details/82801435#1.2%20%E7%89%B9%E7%82%B92%E4%B8%BE%E4%BE%8B>

2 stay Dataframe Use in ix Implement complex slicing
<https://blog.csdn.net/anshuai_aw1/article/details/82801435#2%20%E5%9C%A8Dataframe%E4%B8%AD%E4%BD%BF%E7%94%A8ix%E5%AE%9E%E7%8E%B0%E5%A4%8D%E6%9D%82%E5%88%87%E7%89%87>

3 reference
<https://blog.csdn.net/anshuai_aw1/article/details/82801435#3%20%E5%8F%82%E8%80%83%E6%96%87%E7%8C%AE>

first , This paper introduces the three methods again :

* loc gets rows (or columns) with particular labels from the index. loc From index
Gets the row with a specific label in ( Or column ).
* iloc gets rows (or columns) at particular positions in the index (so it
only takes integers). iloc Gets the row at a specific location in the index ( Or column )( So it only accepts integers ).
* ix usually tries to behave like loc but falls back to behaving like iloc if
a label is not present in the
index. ix It's usually tried to look like loc The same behavior , But if there is no label in the index , It will go back to the image iloc The same behavior .( This sentence is somewhat tongue twisty , No problem , about ix characteristic , We will explain it in detail later )
1 use ix segmentation Series


Please note that : stay pandas edition 0.20.0 And later ,ix It is not recommended , Recommended iloc and loc realization ix. Why is that ? This is because ix The complex characteristics of ix It's a little tricky to use :

* If the index is of integer type , be ix Only label based indexes will be used , It does not fall back to the location-based index . If the label is not in the index , An error is raised .
*
If the index contains more than an integer , Then an integer is given ,ix The location-based index is used immediately instead of the label based index . however , If ix Given another type ( For example, string ), It can use a label based index .
Here's an example 2 Characteristics .

1.1 characteristic 1 give an example
>>> s = pd.Series(np.nan, index=[49,48,47,46,45, 1, 2, 3, 4, 5]) >>> s 49 NaN
48 NaN 47 NaN 46 NaN 45 NaN 1 NaN 2 NaN 3 NaN 4 NaN 5 NaN
Now let's look at using integers 3 What's the result of slicing :

In this case ,s.iloc[:3] Before reading 3 That's ok ( because iloc hold 3 Think of it as a location position), and s.loc[:3]
Read the front 8 That's ok ( because loc hold 3 Label as index label)
>>> s.iloc[:3] # slice the first three rows 49 NaN 48 NaN 47 NaN >>> s.loc[:3]
# slice up to and including label 3 49 NaN 48 NaN 47 NaN 46 NaN 45 NaN 1 NaN 2
NaN 3 NaN >>> s.ix[:3] # the integer is in the index so s.ix[:3] works like loc
49 NaN 48 NaN 47 NaN 46 NaN 45 NaN 1 NaN 2 NaN 3 NaN
be careful :s.ix[:3] The returned results are compared with s.loc[:3] equally , This is because if series If the index is an integer ,ix The first step is to find the tags in the index 3 Instead of looking for a place 3.

If , We tried to find a tag that was not in the index , For example, it is 6 What about ?
>>> s.iloc[:6] 49 NaN 48 NaN 47 NaN 46 NaN 45 NaN 1 NaN >>> s.loc[:6]
KeyError: 6 >>> s.ix[:6] KeyError: 6
In the example above ,s.iloc[:6] As we expected , Back to the front 6 That's ok . and ,s.loc[:6] Back KeyError error , This is because of the label 6 Not in the index .

that ,s.ix[:6] What are the reasons for the error ? As we are in ix Characteristics of 1 That's what it says ,
If the index has only integer type , that ix Use label based index only , It does not fall back to the location-based index . If the label is not in the index , An error is raised .

1.2 characteristic 2 give an example


Let's take an example 1 Come on , If our index is a mixed type , That is, not just integers , Other types are also included , Such as character type . that , to ix An integer number ,ix It will be used immediately iloc operation , Not newspapers KeyError error .
>>> s2 = pd.Series(np.nan, index=['a','b','c','d','e', 1, 2, 3, 4, 5]) >>>
s2.index.is_mixed() # index is mix of different types True >>> s2.ix[:6] # now
behaves like iloc given integer a NaN b NaN c NaN d NaN e NaN 1 NaN
be careful : under these circumstances ,ix Non integers are also acceptable , This is it loc Operation of :
>>> s2.ix[:'c'] # behaves like loc given non-integer a NaN b NaN c NaN
This is an example ix characteristic 2.

As mentioned earlier ,ix It's a bit complicated to use . If only position or label is used for slicing , use iloc perhaps loc That's it , Please avoid using ix.

2 stay Dataframe Use in ix Implement complex slicing

occasionally , in use Dataframe When slicing , We want to use a mixture of labels and positions to slice rows and columns . that , How should it be operated ?

give an example , Consider the following examples Dataframe. We want to get it until the tag is included 'c' Line and front of 4 column .
>>> df = pd.DataFrame(np.nan, index=list('abcde'), columns=['x','y','z', 8,
9]) >>> df x y z 8 9 a NaN NaN NaN NaN NaN b NaN NaN NaN NaN NaN c NaN NaN NaN
NaN NaN d NaN NaN NaN NaN NaN e NaN NaN NaN NaN NaN
stay pandas Earlier versions of (0.20.0) before ,ix This function can be well implemented .

We can use labels to cut branches , Use position to cut columns ( Please note that : because 4 It's not the name of the column , because ix Is used on columns iloc).
>>> df.ix[:'c', :4] x y z 8 a NaN NaN NaN NaN b NaN NaN NaN NaN c NaN NaN NaN
NaN
stay pandas In a later version of , We can use it iloc And other methods can achieve the above functions :
>>> df.iloc[:df.index.get_loc('c') + 1, :4] x y z 8 a NaN NaN NaN NaN b NaN
NaN NaN NaN c NaN NaN NaN NaN
get_loc()
<http://pandas.pydata.org/pandas-docs/version/0.19.1/generated/pandas.Index.get_loc.html>
  Is the method to get the position of the label in the index . Please note that , Because of the use of iloc The last is not included when slicing 1 Points , Because we have to add 1.

You can see , Use only iloc Better use , Because you don't have to pay attention to it ix The 2 individual “ Tedious ” Characteristics of .

3 reference


https://stackoverflow.com/questions/31593201/pandas-iloc-vs-ix-vs-loc-explanation-how-are-they-different

<https://stackoverflow.com/questions/31593201/pandas-iloc-vs-ix-vs-loc-explanation-how-are-they-different>