others - python - 向Dataframe添加新列,並傳遞要填充的變數

我是python的新手,我有一個dataframe:


 name comment 


0 A I enjoy this lovely moment.


1 B I can't believe this was the worst day ever.


2 C The hotel was amazing.


3 A I am overwhelmed by this lovely journey.


4 C Food is good as well as having a nice time.


...



我必須在該dataframe中創建一個新列,並用prediction'的變數填充該列,

希望輸出如下所示:


 name comment prediction


0 A I enjoy this lovely moment. Happy


1 B I can't believe this was the worst day ever. Sad


2 C The hotel was amazing. Happy


3 A I am overwhelmed by this lovely journey. Happy


4 C Food is good as well as having a nice time. happy



...



數據集的代碼是:


rawData = pd.read_csv(io.StringIO(r.decode('utf-8')))



时间:

可以創建一個函數predict,該函數接收一個註釋,並返回預測值,然後使用以下代碼創建一個新列:


rawData['prediction'] = rawData['comment'].apply(lambda x: predict(x))



要在python中創建函數,語法如下:


def predict(comment):


 # code that does the prediction


 return prediction



...