Compare two column value in dataframe and calculate percentage of values similar in two columns

Question:

I am having dataframe df.

I want to compare two columns in dataframe and want to find accuracy or percentage of rows which contain same values in both columns.

df

predicted_value     actual_value     
0                       1
1                       1
0                       0
1                       1

Output- 75% of values are matching between two columns in pandas dataframe.

Asked By: Divyank

||

Answers:

acc = (df.predicted_value==df.actual_value).mean()

if you want to format the accuracy in percentage:

print("%.2f%%"%(acc*100))
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.