I'm working with probabilities that correspond to certain categories and I would like to map them to the categories of interest in a new column of a pandas DataFrame.
I would normally use pandas.Series.map
for such a task but the probabilities have been truncated when processed in another language and so this doesn't work.
I would like to know if it's possible to combine pd.Series.map
and np.isclose
together so that the following example will work as needed? Any alternative approaches would be appreciated also!
import pandas as pd
df = pd.DataFrame({
'a': [1, 2, 3],
'prob': np.round([0.6**(1/30.), 0.9**(1/10.), 0.8**(1/20.)], decimals = 4)
})
prob_dict = {
0.9**(1/10.): 'catA',
0.6**(1/30.): 'catB',
0.8**(1/20.): 'catC'}
df['cat'] = df.prob.map(prob_dict)
>> df
>> a prob cat
>> 0 1 0.983117 NaN
>> 1 2 0.989519 NaN
>> 2 3 0.988905 NaN
My required/needed output is ...
>> df
>> a prob cat
>> 0 1 0.983117 catB
>> 1 2 0.989519 catA
>> 2 3 0.988905 catC