I merged 2 data frames into one on Username
and converted the values into float
. I want to create a new column in which it calculates the percentage.
df1:
Username total_false_positive
x 10
y 20
z 30
s 40
a 50
df2:
Username total_tickets
x 20
y 30
z 40
s 50
a 60
df3 now is
username total_false_positive total_tickets
x 10 20
y 20 30
z 30 40
s 40 50
a 50 60
I tried this:
df3.insert(3, "FPRatio", (df1['total_false_positive']/df2['total_tickets'])*100)
but the results were messed up. not sure what I can do to fix it. Thanks!
CodePudding user response:
I assume that your dataframes do not align as you did not show the "messed up" part. As you already have df3
with the correct data I would base the calculation also on df3
and not on the other to dataframes. Simply replace df1
and df2
by df3
like this:
df3.insert(3, "FPRatio", (df3['total_false_positive']/df3['total_tickets'])*100)
My output:
username total_false_positive total_tickets FPRatio
0 x 10 20 50.000000
1 y 20 30 66.666667
2 z 30 40 75.000000
3 s 40 50 80.000000
4 a 50 60 83.333333