0

I have the following dataframe:

id  val
1   10
1   2012
1   65

i want to convert the val column to create values that add up to 1

df['val'] = np.exp(df.val)/sum(np.exp(df.val))

but i get the following

id  val
1   0.0
1   NaN
1   0.0

how can i take care of this to make probabilities equal to 1 for this case?

Eisen
  • 1,697
  • 9
  • 27

1 Answers1

0

I hope I understood your question correctly.
You can divide each value in the val column by sum of all the values in the val column:

df['val']/df['val'].sum()

0    0.004792
1    0.964063
2    0.031145
Name: val, dtype: float64
theprosty
  • 1
  • 1