Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python Pandas - Create a subset by choosing specific values from columns based on indexes
To create a subset by choosing specific values from columns based on indexes, use the iloc() method. The iloc() method allows you to select data by integer position, making it perfect for choosing specific rows and columns from a DataFrame.
Creating a DataFrame
Let us first create a sample DataFrame with product records ?
import pandas as pd
dataFrame = pd.DataFrame({
"Product": ["SmartTV", "ChromeCast", "Speaker", "Earphone"],
"Opening_Stock": [300, 700, 1200, 1500],
"Closing_Stock": [200, 500, 1000, 900]
})
print("DataFrame...\n", dataFrame)
DataFrame... Closing_Stock Opening_Stock Product 0 200 300 SmartTV 1 500 700 ChromeCast 2 1000 1200 Speaker 3 900 1500 Earphone
Creating Subsets Using iloc()
Single Column Subset
You can select a specific column by its index position ?
import pandas as pd
dataFrame = pd.DataFrame({
"Product": ["SmartTV", "ChromeCast", "Speaker", "Earphone"],
"Opening_Stock": [300, 700, 1200, 1500],
"Closing_Stock": [200, 500, 1000, 900]
})
# Select first column (Product) for all rows
subset_column = dataFrame.iloc[:, 0]
print("Single column subset:\n", subset_column)
Single column subset: 0 SmartTV 1 ChromeCast 2 Speaker 3 Earphone Name: Product, dtype: object
Multiple Columns and Rows Subset
Create a subset with specific rows and columns using row and column index ranges ?
import pandas as pd
dataFrame = pd.DataFrame({
"Product": ["SmartTV", "ChromeCast", "Speaker", "Earphone"],
"Opening_Stock": [300, 700, 1200, 1500],
"Closing_Stock": [200, 500, 1000, 900]
})
# Create subset with first 2 rows and first 2 columns
subset_multiple = dataFrame.iloc[0:2, 0:2]
print("Subset using iloc() with 2 rows and 2 columns:\n", subset_multiple)
Subset using iloc() with 2 rows and 2 columns:
Product Opening_Stock
0 SmartTV 300
1 ChromeCast 700
Specific Rows and Columns
Select non-consecutive rows and columns using lists of indexes ?
import pandas as pd
dataFrame = pd.DataFrame({
"Product": ["SmartTV", "ChromeCast", "Speaker", "Earphone"],
"Opening_Stock": [300, 700, 1200, 1500],
"Closing_Stock": [200, 500, 1000, 900]
})
# Select specific rows (0, 2) and columns (0, 2)
subset_specific = dataFrame.iloc[[0, 2], [0, 2]]
print("Specific rows and columns:\n", subset_specific)
Specific rows and columns: Product Closing_Stock 0 SmartTV 200 2 Speaker 1000
Key Points
-
iloc[row_indexer, column_indexer]uses integer positions - Use slices like
0:2for ranges (excludes the end index) - Use lists like
[0, 2]for specific non-consecutive positions - Use
:to select all rows or columns
Conclusion
The iloc() method provides flexible integer-based indexing for creating DataFrame subsets. Use slices for consecutive elements or lists for specific positions to extract exactly the data you need.
