

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python Pandas - Get integer location for requested label and find the previous index value if no exact match
<p>To get integer location for requested label and find the previous index value if no exact match, use the <strong>index.get_loc()</strong>. Set the parameter <strong>method</strong> to the value <strong>ffill</strong>.</p><p>At first, import the required libraries −</p><pre class="just-code notranslate language-python3" data-lang="python3">import pandas as pd</pre><p>Creating Pandas index −</p><pre class="just-code notranslate language-python3" data-lang="python3">index = pd.Index([10, 20, 30, 40, 50, 60, 70])</pre><p>Display the Pandas index −</p><pre class="just-code notranslate language-python3" data-lang="python3">print("Pandas Index... ",index)</pre><p>Get the location of the previous index if no exact match. The value is set "ffill" using the "method" parameter of the get_loc() −</p><pre class="just-code notranslate language-python3python" data-lang="python3">print(" Get the location of the previous index if no exact match... ", index.get_loc(45, method="ffill"))</pre><h2>Example</h2><p>Following is the code −</p><pre class="demo-code notranslate language-python3" data-lang="python3">import pandas as pd # Creating Pandas index index = pd.Index([10, 20, 30, 40, 50, 60, 70]) # Display the Pandas index print("Pandas Index... ",index) # Return the number of elements in the Index print(" Number of elements in the index... ",index.size) # get integer location from the given index print(" Display integer location from given index... ",index.get_loc(20)) print(" Display integer location from given index... ",index.get_loc(50)) # Get the location of the previous index if no exact match # The value is set "ffill" using the "method" parameter of the get_loc() print(" Get the location of the previous index if no exact match... ", index.get_loc(45, method="ffill"))</pre><h2>Output</h2><p>This will produce the following output −</p><pre class="result notranslate">Pandas Index... Int64Index([10, 20, 30, 40, 50, 60, 70], dtype='int64') Number of elements in the index... 7 Display integer location from given index... 1 Display integer location from given index... 4 Get the location of the previous index if no exact match... 3</pre>
- Related Questions & Answers
- Python Pandas - Get integer location for requested label and find the nearest index value if no exact match
- Python Pandas - Compute indexer and find the previous index value if no exact match
- Python Pandas - Get integer location for requested label
- Python Pandas IntervalIndex - Get integer location for requested label
- Python Pandas - Compute indexer and find the next index value if no exact match
- Python Pandas - Compute indexer and find the nearest index value if no exact match
- Python Pandas - Get location and sliced index for requested label/ level in a MultiIndex
- Python Pandas - Get location and sliced index for requested label/ level but do not drop the level
- Python Pandas - Return the label from the index or if not present, the previous one
- Python Pandas - Get the codes (location of each label) in MultiIndex
- Python Pandas - Return vector of label values for requested level in a MultiIndex
- Python Pandas - Get location for a label or a tuple of labels in a MultiIndex
- MongoDB query for exact match
- Python Pandas - Return the label from the index if all of the labels in the index are later than the passed label
- Python - Make new Pandas Index with passed location deleted
Advertisements