Extracting rows using Pandas .iloc[]
0 604
Mastering Row Extraction with Pandas .iloc[]
In data analysis, efficiently accessing specific rows in a dataset is crucial. Pandas' .iloc[] method provides a powerful way to retrieve rows based on integer positions. This guide explores various techniques to extract rows using .iloc[] in Pandas.
What is .iloc[]?
The .iloc[] accessor in Pandas is integer-location based, meaning it allows you to select rows and columns by their numerical positions. This contrasts with .loc[], which uses label-based indexing. Understanding when and how to use .iloc[] is essential for effective data manipulation.
Selecting a Single Row
To retrieve a single row, pass the integer index to .iloc[]:
import pandas as pd
# Sample DataFrame
data = {'Name': ['Alice', 'Bob', 'Charlie'],
'Age': [25, 30, 35],
'City': ['New York', 'Los Angeles', 'Chicago']}
df = pd.DataFrame(data)
# Retrieve row at index 1
bob_data = df.iloc[1]
print(bob_data)
This will output the data for 'Bob' as a Series. Note that the index 1 corresponds to the second row, as indexing starts from 0.
Selecting Multiple Rows
To select multiple rows, pass a list of integer indices:
# Retrieve rows at indices 0 and 2
subset = df.iloc[[0, 2]]
print(subset)
This returns a DataFrame containing the rows for 'Alice' and 'Charlie'.
Selecting Rows by Index Range
When the index is ordered, you can select a range of rows:
# Retrieve rows from index 1 to 2
range_subset = df.iloc[1:3]
print(range_subset)
Note that the end index is exclusive, so this selects rows at indices 1 and 2.
Selecting Specific Columns
To select specific columns along with rows:
# Select 'Age' and 'City' columns for row at index 1
bob_info = df.iloc[1, [1, 2]]
print(bob_info)
This returns a Series with the 'Age' and 'City' information for 'Bob'.
Modifying Rows
You can also modify rows using .iloc[]:
# Update 'Age' for row at index 0
df.iloc[0, 1] = 26
print(df.iloc[0])
This updates the 'Age' value for 'Alice' to 26.
Conclusion
The .iloc[] method in Pandas is a versatile tool for row selection and manipulation based on integer positions. By understanding its capabilities, you can efficiently access and modify data in your DataFrames. Remember to ensure that the integer indices you reference exist within the DataFrame to avoid errors.
If you’re passionate about building a successful blogging website, check out this helpful guide at Coding Tag – How to Start a Successful Blog. It offers practical steps and expert tips to kickstart your blogging journey!
For dedicated UPSC exam preparation, we highly recommend visiting www.iasmania.com. It offers well-structured resources, current affairs, and subject-wise notes tailored specifically for aspirants. Start your journey today!
Share:



Comments
Waiting for your comments