【问题标题】:Pandas how to index a a list from JSON and put it into a dataframe?Pandas 如何从 JSON 索引列表并将其放入数据框中?
【发布时间】:2021-04-21 06:54:52
【问题描述】:

如何在数据框中索引列表?

我这里有这段代码,它将从 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]

有什么方法可以做到吗?

【问题讨论】:

  • 您是否可以将数据帧发布为代码/csv 而不是图像?谢谢。
  • @Ankur 添加了它:)

标签: python json pandas numpy sentiment-analysis


【解决方案1】:

在您的代码中,您已经决定了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),'Result': result, 'Probability': max(proba}, ignore_index = True)

然后将input_c 中的索引设置为Probability 列,使用

input_c.set_index('Probability')

【讨论】:

    猜你喜欢
    • 2021-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-29
    • 2021-06-22
    • 1970-01-01
    • 2017-12-31
    相关资源
    最近更新 更多