Write a Python program to generate random ten rows, two columns of vowels. If both the rows are matched with same vowels, then print the index and count of matched columns

In this tutorial, we'll create a Python program that generates a DataFrame with ten rows and two columns of random vowels, then finds rows where both columns have matching vowels and counts them.

Problem Overview

We need to ?

  • Generate a DataFrame with 10 rows and 2 columns containing random vowels

  • Find rows where both columns have the same vowel

  • Display the matched rows and count them

Sample DataFrame

Here's an example of what our DataFrame might look like ?

   col1 col2
0    o    e
1    e    e
2    i    u
3    e    o
4    i    i
5    u    o
6    e    a
7    u    o
8    a    u
9    e    a

Expected Output

For the above DataFrame, the matched rows and count would be ?

Matched rows:
   col1 col2
1    e    e
4    i    i
Count: 2

Solution

To solve this problem, we will follow these steps ?

  • Import required libraries (pandas and numpy)

  • Create a DataFrame with random vowels using np.random.choice()

  • Find matching rows using boolean indexing: df[df.col1 == df.col2]

  • Count the matched rows using len()

Implementation

import pandas as pd
import numpy as np

# Generate DataFrame with random vowels
df = pd.DataFrame({
    'col1': np.random.choice(['a', 'e', 'i', 'o', 'u'], 10), 
    'col2': np.random.choice(['a', 'e', 'i', 'o', 'u'], 10)
})

print("DataFrame is:")
print(df)
print()

# Find rows where both columns match
matched_rows = df[df.col1 == df.col2]
print("Matched rows:")
print(matched_rows)
print()

# Count matched rows
count = len(matched_rows)
print("Count of matched rows:", count)
DataFrame is:
   col1 col2
0    o    e
1    e    e
2    i    u
3    e    o
4    i    i
5    u    o
6    e    a
7    u    o
8    a    u
9    e    a

Matched rows:
   col1 col2
1    e    e
4    i    i

Count of matched rows: 2

How It Works

  • np.random.choice() − Randomly selects vowels from the list for each column

  • df.col1 == df.col2 − Creates a boolean mask comparing values in both columns

  • df[boolean_mask] − Filters the DataFrame to show only matching rows

  • len() − Counts the number of matched rows

Alternative Method Using iloc and np.where

You can also use np.where() with iloc for the same result ?

import pandas as pd
import numpy as np

# Generate DataFrame with random vowels
df = pd.DataFrame({
    'col1': np.random.choice(['a', 'e', 'i', 'o', 'u'], 10), 
    'col2': np.random.choice(['a', 'e', 'i', 'o', 'u'], 10)
})

print("DataFrame is:")
print(df)
print()

# Using np.where and iloc
matched_indices = np.where(df.col1 == df.col2)
matched_rows = df.iloc[matched_indices]

print("Matched rows:")
print(matched_rows)
print()

print("Count:", len(matched_rows))
DataFrame is:
   col1 col2
0    u    e
1    a    a
2    e    i
3    o    u
4    i    i
5    a    e
6    u    u
7    e    o
8    i    a
9    o    e

Matched rows:
   col1 col2
1    a    a
4    i    i
6    u    u

Count: 3

Conclusion

This program demonstrates how to generate random data using NumPy and filter DataFrames using boolean indexing in Pandas. The boolean comparison df.col1 == df.col2 provides an efficient way to find matching rows and count them.

Updated on: 2026-03-25T16:16:55+05:30

131 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements