如何解决Pandas 如何从 JSON 索引一个列表并将其放入数据帧中?
如何索引数据框内的列表?
我这里有这段代码,它将从 JSON 中获取数据并将其插入到数据帧中
这是 JSON 的样子
{"text_sentiment": "positive","text_probability": [0.33917574607174916,0.26495590980799744,0.3958683441202534]}
这是我的代码。
input_c = pd.DataFrame(columns=['Comments','Result'])
for i in range(input_df.shape[0]):
url = 'http://classify/?text='+str(input_df.iloc[i])
r = requests.get(url)
result = r.json()["text_sentiment"]
proba = r.json()["text_probability"]
input_c = input_c.append({'Comments': input_df.loc[i].to_string(index=False),'Result': result,'Probability': proba},ignore_index = True)
st.write(input_c)
结果如下 result
Comments Result Probability
0 This movie is good in my eyes. neutral [0.26361889609129974,0.4879752378104797,0.2484058660982205]
1 This is a bad movie it's not good. negative [0.5210904912792065,0.22073131008688818,0.25817819863390534]
2 One of the best performance in this year. positive [0.14644707145500369,0.3581522311734714,0.49540069737152503]
3 The best movie i've ever seen. positive [0.1772046003747405,0.026468108571479156,0.7963272910537804]
4 The movie is meh. neutral [0.24349393167653663,0.6820982528652574,0.07440781545820596]
5 One of the best selling artist in the world. positive [0.07738688706903311,0.3329095061233371,0.5897036068076298]
概率列中的数据是我想要索引的数据。
例如:如果结果中的值为“正”,那么我希望概率指数为 2,如果结果为“中性”指数为 1
像这样
Comments Result Probability
0 This movie is good in my eyes. neutral [0.4879752378104797]
1 This is a bad movie it's not good. negative [0.5210904912792065]
2 One of the best performance in this year. positive [0.49540069737152503]
3 The best movie i've ever seen. positive [0.7963272910537804]
4 The movie is meh. neutral [0.6820982528652574]
5 One of the best selling artist in the world. positive [0.5897036068076298]
有什么方法可以做到吗?
解决方法
在您的代码中,您已经决定了 Result
的内容,无论是负数、中性还是正数,因此您只需将概率列表的最大值存储在数据框 input_c
中。
这意味着,将 'Probability': proba
更改为 'Probability': max(proba)
,因此修改:
input_c = input_c.append({'Comments': input_df.loc[i].to_string(index=False),'Result': result,'Probability': proba},ignore_index = True)
到
input_c = input_c.append({'Comments': input_df.loc[i].to_string(index=False),'Probability': max(proba},ignore_index = True)
然后将input_c
中的索引设置为Probability
列,使用
input_c.set_index('Probability')
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。