A simple data frame. I want to have a percentage to show the number of rows in the column "Tested" of 22, over the total number of rows.
i.e. 1. there are 5 rows of 22 in the column "Tested"
- the data frame total of 15 rows
So the percentage is 5/15 = 0.33
I tried below, but it gives zero.
How can I correct it? Thank you.
import pandas as pd
data = {'Unit_Weight': [335,335,119,119,52,452,19,19,19,165,165,165,724,724,16],
'Tested' : [22,12,14,16,18,20,22,24,26,28,22,22,48,50,22]}
df = pd.DataFrame(data)
num_row = df.shape[0]
suspect_row = df[df["Tested"] == 22].shape[0]
suspect_over_total = suspect_row/num_row
print num_row # 15
print suspect_row # 5
print float(suspect_over_total) # 0.0