Note that in the last example, data.loc[487] (the row with index value 487) is not equal to data.iloc[487] (the 487th row in the data). The index of the DataFrame can be out of numeric order, and/or a string or multi-value.
2b. Boolean / Logical indexing using .loc
Conditional selections with boolean arrays using data.loc[<selection>] is the most common method that I use with Pandas DataFrames. With boolean indexing or logical selection, you pass an array or Series of True/False values to the .loc indexer to select the rows where your Series has True values.
In most use cases, you will make selections based on the values of different columns in your data set.
For example, the statement data[‘first_name’] == ‘Antonio’] produces a Pandas Series with a True/False value for every row in the ‘data’ DataFrame, where there are “True” values for the rows where the first_name is “Antonio”. These type of boolean arrays can be passed directly to the .loc indexer as so:
As before, a second argument can be passed to .loc to select particular columns out of the data frame. Again, columns are referred to by name for the loc indexer and can be a single string, a list of columns, or a slice “:” operation.
Logical selections and boolean Series can also be passed to the generic [] indexer of a pandas DataFrame and will give the same results: data.loc[data[‘id’] == 9] == data[data[‘id’] == 9] .
3. Selecting pandas data using ix
Note: The ix indexer has been deprecated in recent versions of Pandas, starting with version 0.20.1.
The ix[] indexer is a hybrid of .loc and .iloc. Generally, ix is label based and acts just as the .loc indexer. However, .ix also supports integer type selections (as in .iloc) where passed an integer. This only works where the index of the DataFrame is not integer based. ix will accept any of the inputs of .loc and .iloc.
Slightly more complex, I prefer to explicitly use .iloc and .loc to avoid unexpected results.
As an example:
# ix indexing works just the same as .loc when passed strings
data.ix[['Andrade']] == data.loc[['Andrade']]
# ix indexing works the same as .iloc when passed integers.
data.ix[[33]] == data.iloc[[33]]
# ix only works in both modes when the index of the DataFrame is NOT an integer itself.
</div>
<div class="gist-meta">
<a href="https://gist.github.com/shanealynn/2c61235a30cbe1dff15d81b90c216cfe/raw/5e31330041dcecfffa4f4552a86aa46b5d427bf0/Pandas%20index%20-%20ix%20selections.py" style="float:right">view raw</a>
<a href="https://gist.github.com/shanealynn/2c61235a30cbe1dff15d81b90c216cfe#file-pandas-index-ix-selections-py">Pandas index - ix selections.py</a>
hosted with <img draggable="false" class="emoji" alt="❤" src="https://s.w.org/images/core/emoji/12.0.0-1/svg/2764.svg" scale="0"> by <a href="https://github.com">GitHub</a>
</div>
</div>
With a slight change of syntax, you can actually update your DataFrame in the same statement as you select and filter using .loc indexer. This particular pattern allows you to update values in columns depending on different conditions. The setting operation does not make a copy of the data frame, but edits the original data.
As an example:
# Change the first name of all rows with an ID greater than 2000 to "John"
data.loc[data['id'] >2000, "first_name"] ="John"
# Change the first name of all rows with an ID greater than 2000 to "John"
data.loc[data['id'] >2000, "first_name"] ="John"
</div>
<div class="gist-meta">
<a href="https://gist.github.com/shanealynn/1c30da11d88cba2ecdf3936209a981b9/raw/dc82d5f8b119343bfe17849f4ac97aebd82b5eb0/Pandas%20index%20-%20changing%20data%20with%20loc.py" style="float:right">view raw</a>
<a href="https://gist.github.com/shanealynn/1c30da11d88cba2ecdf3936209a981b9#file-pandas-index-changing-data-with-loc-py">Pandas index - changing data with loc.py</a>
hosted with <img draggable="false" class="emoji" alt="❤" src="https://s.w.org/images/core/emoji/12.0.0-1/svg/2764.svg" scale="0"> by <a href="https://github.com">GitHub</a>
</div>
</div>
The Pandas DataFrame - this blog post covers the basics of loading, editing, and viewing data in Python, and getting to grips with the all-important data structure in Python - the Pandas Dataframe. Learn by example to load CSV files, rename columns, extract statistics, and select rows and columns." rel=“nofollow” data-origin=“643” data-position=“0”>
The Pandas DataFrame - this blog post covers the basics of loading, editing, and viewing data in Python, and getting to grips with the all-important data structure in Python - the Pandas Dataframe. Learn by example to load CSV files, rename columns, extract statistics, and select rows and columns." rel=“nofollow” data-origin=“643” data-position=“0”>The Pandas DataFrame - loading, editing, and viewing data in Python
The Pandas DataFrame - this blog post covers the basics of loading, editing, and viewing data in Python, and getting to grips with the all-important data structure in Python - the Pandas Dataframe. Learn by example to load CSV files, rename columns, extract statistics, and select rows and columns.
Pandas - Python Data Analysis Library I’ve recently started using Python’s excellent Pandas library as a data analysis tool, and, while finding the transition from R’s excellent data.table library frustrating at times, I’m finding my way around and finding most things work quite well. One aspect that I’ve recently been…" rel=“nofollow” data-origin=“643” data-position=“1”>
Pandas - Python Data Analysis Library I’ve recently started using Python’s excellent Pandas library as a data analysis tool, and, while finding the transition from R’s excellent data.table library frustrating at times, I’m finding my way around and finding most things work quite well. One aspect that I’ve recently been…" rel=“nofollow” data-origin=“643” data-position=“1”>Summarising, Aggregating, and Grouping data in Python Pandas
Pandas - Python Data Analysis Library I’ve recently started using Python’s excellent Pandas library as a data analysis tool, and, while finding the transition from R’s excellent data.table library frustrating at times, I’m finding my way around and finding most things work quite well. One aspect that I’ve recently been…
Merging and Joining data sets are key activities of any data scientist or analyst. In this tutorial, we explore the process of combining datasets based on common columns quickly and easily with the Python Pandas library and it’s fast merge() functionality. Finally conquer merging and become a master with this…" rel=“nofollow” data-origin=“643” data-position=“2”>
Merging and Joining data sets are key activities of any data scientist or analyst. In this tutorial, we explore the process of combining datasets based on common columns quickly and easily with the Python Pandas library and it’s fast merge() functionality. Finally conquer merging and become a master with this…" rel=“nofollow” data-origin=“643” data-position=“2”>Merge and Join DataFrames with Pandas in Python
Merging and Joining data sets are key activities of any data scientist or analyst. In this tutorial, we explore the process of combining datasets based on common columns quickly and easily with the Python Pandas library and it’s fast merge() functionality. Finally conquer merging and become a master with this…
March 5, 2017
In "blog"
78 thoughts on “Using iloc, loc, & ix to select rows and columns in Pandas DataFrames”
Pingback:在Python中实现l’algoritmo KNN scikit-learn | 洛伦佐·戈沃尼
对初学者来说真的很有帮助。非常详细。在pandas和python上寻找更多关于你的博客。