2

It's probably pretty obvious in the end but I cannot think of a way to do this. Thanks for your help!

I did a prediction task and as a result I have a DataFrame with the percentages and a column with the predicted class, like so:

 Class1 Class2 Class3 Prediction 0 0.99 0.01 0.00 Class1 1 0.15 0.14 0.71 Class3 2 0.05 0.80 0.15 Class2 

Now I want to access the probability with which a class was predicted. So I want to have a list like below so I can work with it further.

0 0.99 1 0.71 2 0.80 

I have problems finding a way to access only one value of df.Predicted at a time and have no idea how to search for it. How do I get this value or alternatively my desired list? I tried this:

values = [] for row in df.Predicted: values.append(row) print(values) 

but it returns the whole column for each iteration. It also doesn't feel very pandas-like. I am using python 3.5 in case it makes a difference

1 Answer 1

1

IIUC:

In [15]: df.lookup(df.index, df.Prediction) Out[15]: array([ 0.99, 0.71, 0.8 ]) 

or

In [23]: df.max(axis=1) Out[23]: 0 0.99 1 0.71 2 0.80 dtype: float64 

or

In [24]: df.filter(regex='^Class').idxmax(axis=1) Out[24]: 0 Class1 1 Class3 2 Class2 dtype: object 
Sign up to request clarification or add additional context in comments.

2 Comments

That works. Thank you! I will accept your answer as soon as the time limit runs out
@Anita219, glad it helps :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.