# 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

PythonPandasServer Side ProgrammingProgramming

#### Beyond Basic Programming - Intermediate Python

Most Popular

36 Lectures 3 hours

#### Practical Machine Learning using Python

Best Seller

91 Lectures 23.5 hours

#### Practical Data Science using Python

22 Lectures 6 hours

Assume, you have a dataframe,

 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

The result for matched index and count is,

index is
col1 col2
1 e    e
4 i    i
count is 2

## Solution

To solve this, we will follow the steps given below −

• Define a dataframe

• Compare first and second matching index values using the below method,

df.iloc[np.where(df.col1==df.col2)])
• Find the total count of matched columns using the below step,

len(df.iloc[np.where(df.col1==df.col2)])

### Example

Let us see the following implementation to get a better understanding −

import pandas as pd
import numpy as np
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 ", df)
print("index is ",df.iloc[np.where(df.col1==df.col2)])
print("count is ", len(df.iloc[np.where(df.col1==df.col2)]))

### Output

 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
index is
col1 col2
1 e    e
4 i    i
count is 2
Updated on 24-Feb-2021 09:38:34