1

I have these 2 dataframes

dffouten:

                     index          opmerking
DateTime                                     
2018-11-01 08:05:41     20         photocells
2018-11-01 11:40:55     42  trap/roodnoodstop
2018-11-02 07:24:02     62  trap/roodnoodstop

and

dffm:

                      Counter
traploopext             4

What i want to do is divide the amount of times trap/noodstop by the traploopext and countervalue of 4 so what I did is:

dffouten = dffouten.groupby('opmerking').count()

which gives me

                   index
opmerking               
photocells             1
trap/roodnoodstop      2

and then

percentage = (dffm.loc['rectloopext'] / dffouten.loc['trap/roodnoodstop']) * 100

but this doesnt work, the strange thing to me is that if use :

percentage = (dffm.loc['rectloopext'] / 2) * 100

that it gives me the answer.

1 Answers1

0

It seems is necessary specify column in function loc for return scalar, for count values per column is used value_counts so for scalar is used loc and finally get division between 2 scalars:

dffouten = dffouten['opmerking'].value_counts()
print (dffouten)
trap/roodnoodstop    2
photocells           1
Name: opmerking, dtype: int64

#for Series select by index
print (dffouten.loc['trap/roodnoodstop'])
2

#for DataFrame select by index and column
print (dffm.loc['traploopext', 'Counter'])
4

percentage = (dffm.loc['traploopext', 'Counter'] / dffouten.loc['trap/roodnoodstop']) * 100
print (percentage)
200.0

More information is possible find here

jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252