Baktaawar
Baktaawar

Reputation: 7500

Dividing two pandas values

I have the following dataset from the following commands

df.ab.value_counts()

Out[154]:

0    31196
1    18804
dtype: int64

I want to find the fraction of 1 out of total counts. So basically 18804/50000

I do the following:

(df.ab.value_counts()[1])/(df.ab.count())

Out[146]:

0

As you see it gives me zero while it should be 0.3768 (18804/50000)

Any idea why?

Edit II:

Any idea how to plot the bar graph of these two values 0 and 1 and their counts?

Upvotes: 1

Views: 857

Answers (3)

Satya
Satya

Reputation: 5907

from __future__ import division
(df.ab.value_counts()[1])/(df.ab.count())  ###should give float value 0.3768

Have not tested it, but hopes it will help you.

Upvotes: 0

knagaev
knagaev

Reputation: 2957

You can do it easier

df.ab.value_counts(normalize=True)

Upvotes: 0

jezrael
jezrael

Reputation: 863166

I think you need add casting to float:

print (df.ab.value_counts()[1])/float(df.ab.count())
0.37608

print 18804/50000
0
print 18804/float(50000)
0.37608
print float(18804)/50000
0.37608

Upvotes: 2

Related Questions