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
Write a program in Python to select any random odd index rows in a given DataFrame
In data analysis, you often need to randomly select rows from a DataFrame based on certain criteria. This tutorial shows how to select a random row from odd-indexed positions (1, 3, 5, etc.) in a Pandas DataFrame.
Sample DataFrame
Let's start with a sample DataFrame to demonstrate the concept ?
import pandas as pd
import random
df = pd.DataFrame({
'id': [1, 2, 3, 4, 5, 6, 7],
'mark': [70, 60, 40, 50, 80, 90, 60],
'age': [12, 13, 12, 13, 12, 13, 12]
})
print("DataFrame is:")
print(df)
DataFrame is: id mark age 0 1 70 12 1 2 60 13 2 3 40 12 3 4 50 13 4 5 80 12 5 6 90 13 6 7 60 12
Method 1: Using Loop to Find Odd Indices
This approach iterates through all indices and collects odd-indexed positions ?
import pandas as pd
import random
df = pd.DataFrame({
'id': [1, 2, 3, 4, 5, 6, 7],
'mark': [70, 60, 40, 50, 80, 90, 60],
'age': [12, 13, 12, 13, 12, 13, 12]
})
# Collect odd indices
odd_indices = []
for i in df.index.values:
if i % 2 == 1:
odd_indices.append(i)
# Select random odd index
random_index = random.choice(odd_indices)
print("Random odd index row is:")
print(df.iloc[random_index])
Random odd index row is: id 4 mark 50 age 13 Name: 3, dtype: int64
Method 2: Using List Comprehension
A more concise approach using list comprehension to filter odd indices ?
import pandas as pd
import random
df = pd.DataFrame({
'id': [1, 2, 3, 4, 5, 6, 7],
'mark': [70, 60, 40, 50, 80, 90, 60],
'age': [12, 13, 12, 13, 12, 13, 12]
})
# Get odd indices using list comprehension
odd_indices = [i for i in df.index if i % 2 == 1]
print("Odd indices:", odd_indices)
# Select random odd index
random_index = random.choice(odd_indices)
print("Selected index:", random_index)
print("Random odd index row:")
print(df.iloc[random_index])
Odd indices: [1, 3, 5] Selected index: 5 Random odd index row: id 6 mark 90 age 13 Name: 5, dtype: int64
Method 3: Direct Slicing with Random Selection
Use DataFrame slicing to get odd rows directly, then randomly select one ?
import pandas as pd
import random
df = pd.DataFrame({
'id': [1, 2, 3, 4, 5, 6, 7],
'mark': [70, 60, 40, 50, 80, 90, 60],
'age': [12, 13, 12, 13, 12, 13, 12]
})
# Get all odd-indexed rows
odd_rows = df.iloc[1::2] # Start from index 1, step by 2
print("All odd-indexed rows:")
print(odd_rows)
# Select random row from odd rows
random_row = odd_rows.sample(n=1)
print("\nRandom odd index row:")
print(random_row.iloc[0])
All odd-indexed rows: id mark age 1 2 60 13 3 4 50 13 5 6 90 13 Random odd index row: id 2 mark 60 age 13 Name: 1, dtype: int64
Comparison
| Method | Code Complexity | Performance | Best For |
|---|---|---|---|
| Loop with modulo | Medium | Slower for large data | Educational purposes |
| List comprehension | Low | Medium | Readable and concise |
| Direct slicing | Low | Fastest | Large DataFrames |
Conclusion
Use direct slicing with sample() for the most efficient approach. List comprehension offers a good balance of readability and performance. The loop method helps understand the logic but is less efficient for large datasets.
