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 \n", df) print("index is \n",df.iloc[np.where(df.col1==df.col2)]) print("count is \n", 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