我們有以下虛擬資料幀,可以抓取許多訊息:
temp = pd.DataFrame(np.array([['I am feeling very well',],['It is hard to believe this happened',],
['What is love?',], ['Amazing day today',]]),
columns = ['message',])
輸出:
message
0 I hate the weather today
1 It is hard to believe this happened
2 What is love
3 Amazing day today
我遍歷每個單獨的訊息,以便從中提取情緒
for i in temp.message:
x = model.predict(i, 'roberta')
其中x是以下形式的字典:
x = {
"Love" : 0.0931,
"Hate" : 0.9169,
}
如何在迭代每個值時將字典中的所有值添加到資料框中?
for i in temp.message:
x = model.predict(i, 'roberta')
y = pd.DataFrame.from_dict(x,orient='index')
y = y.T
# what would the next step be?
也許創建具有空值的列,然后在訊息列的每次迭代上創建左連接將是一個合理的解決方案?什么是最優化的?
預期輸出:
message Love Hate
0 I hate the weather today 0.0931 0.9169
1 It is hard to believe this happened 0.444 0.556
...
uj5u.com熱心網友回復:
不要嘗試在回圈時分配,在串列中收集并在最后分配/加入:
df = temp.join(pd.json_normalize([model.predict(i, 'roberta')
for i in temp.message]))
# OR
df = temp.join(pd.DataFrame([model.predict(i, 'roberta')
for i in temp.message]))
例子:
message Love Hate
0 I am feeling very well 0.0931 0.9169
1 It is hard to believe this happened 0.0931 0.9169
2 What is love? 0.0931 0.9169
3 Amazing day today 0.0931 0.9169
uj5u.com熱心網友回復:
您可以np.NaN最初創建具有值的列,并在必要時更新
temp = pd.DataFrame(np.array([['I am feeling very well',],['It is hard to believe this happened',],
['What is love?',], ['Amazing day today',]]),
columns = ['message',])
temp['Love'] = np.nan
temp['Hate'] = np.nan
然后更新回圈中的值 -
for i, message in enumerate(temp.message):
x = model. Predictt(message, 'Roberta')
temp.loc[index].Love = x["Love"]
temp.loc[index].Hate= x["Hate"]
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/526385.html
標籤:Python熊猫麻木的
上一篇:無法進入Jenkinsfile中IfElse條件的“else”塊
下一篇:Python中的巧妙樞軸和映射
