Summing matching dataframe column values ​​with Series values

I have a series:

350    0
254    1
490    0
688    0
393    1
30     1

and data frame:

       0   outcome
0    350     1
1    254     1
2    490     0
3    688     0
4    393     0
5     30     1

Below is the code to calculate the total number of matches between the “Series” column and the “Result” in the data frame - this is what was intended. Is there any other better way besides below?

i=0
match=0
for pred in results['outcome']:
    if test.values[i] == pred:
        match+=1
    i+=1
print match

I tried to use results['Survived'].eq(labels_test).sum(), but the answer is incorrect. And using lambda, but the syntax is wrong.

please inform. thank

+4
source share
2 answers

You can compare by matching the ie series

(df['0'].map(s) == df['outcome']).sum()

4
+4
source

First align the frame and series with align.

df, s = df.set_index('0').align(s, axis=0)

outcome s True -

df.outcome.eq(s).sum()
4
+2

Source: https://habr.com/ru/post/1691327/


All Articles