Chris90
Chris90

Reputation: 1998

Dividing the sum of values of one column by count of all rows in the dataframe

Had a simple question, I am trying to get a rate or % by dividing the sum of one column ( Values ) by all the rows in that df.

   DF:

    ID | Values
     D      1
     B      0
     R      1
etc...

I want to take the sum of Values column an divide by count or number of rows inn the df.

Tryin something like

df[(df.Values.sum()] / len(df.ID)

Not sure if I am on right track

thanks!

Upvotes: 1

Views: 933

Answers (2)

Ankur Sinha
Ankur Sinha

Reputation: 6639

Scott's method is stellar and I would do the same as him. However, correcting your code would be:

df['values'].sum() / len(df)

Upvotes: 2

Scott Boston
Scott Boston

Reputation: 153460

Use mean:

df['Values'].mean()

Output:

0.66666666666

Upvotes: 4

Related Questions