I built a tensorflow model to make text classification in four category, after testing and evaluating it, I need to apply it to actual data to predict the class of them, I create a predict function that return the probability of each class that this text can be. I read my data and apply prediction function using pandas.

df.apply(lambda x: predict(x['text']), axis=1)

what I need is to append predictions value to my original data frame such as:

text        class1_prob.   class2_prob     class3_prob     class4_prob
------      ------------   -----------     ------------     -----------  
1st string  0.1            0.2              0.4             0.3

How can I achieve that if my prediction function return probabilities for one string as:

[[0.1 0.2 0.4 0.3]]

1 Answer 1


After searching I found a way by use pd.concatenate to concatenate the predictions dataframe with original data frame

predictions = df.apply(lambda x: predict(x['text']), axis=1)
predictions_df = pd.DataFrame(predictions.tolist(), columns=\ 
['class1_prob', 'class2_prob', 'class3_prob', 'class4_prob'])
df = pd.concat([df, predictions_df], axis=1)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.