- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- 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 - How to select rows from a DataFrame by integer location
To select rows by integer location, use the iloc() function. Mention the index number of the row you want to select.
Create a DataFrame −
dataFrame = pd.DataFrame([[10, 15], [20, 25], [30, 35]],index=['x', 'y', 'z'],columns=['a', 'b'])
Select rows with integer location using iloc() −
dataFrame.iloc[1]
Example
Following is the code −
import pandas as pd # Create DataFrame dataFrame = pd.DataFrame([[10, 15], [20, 25], [30, 35]],index=['x', 'y', 'z'],columns=['a', 'b']) # DataFrame print"DataFrame...\n",dataFrame # select rows with loc print"\nSelect rows by passing label..." print(dataFrame.loc['z']) # select rows with integer location using iloc print"\nSelect rows by passing integer location..." print(dataFrame.iloc[1])
Output
This will produce the following output −
DataFrame... a b x 10 15 y 20 25 z 30 35 Select rows by passing label... a 30 b 35 Name: z, dtype: int64 Select rows by passing integer location... a 20 b 25 Name: y, dtype: int64
Advertisements