【问题标题】:Select particular row from a DataFrame based on value in a column in pandas根据 Pandas 中列中的值从 DataFrame 中选择特定行
【发布时间】:2018-09-04 18:17:37
【问题描述】:

我正在做情绪分析项目。我得到了结果。我数了数正数、中性数和负数。

这是代码

ltweet.groupby(['sentiment_type'])['sentiment_neutral'].count()


# ltweet= the name of object
# sentiment_type= column
#sentiment_neutral= column

这是输出

Out[105]:
sentiment_type
NEGATIVE     280
NEUTRAL     1308
POSITIVE    1193
Name: sentiment_neutral, dtype: int64

我只想显示“积极”(不是消极和中性)

我尝试了其他代码,但仍然出现错误。谁能帮我这个?

【问题讨论】:

  • s = ltweet.groupby(['sentiment_type'])['sentiment_neutral'].count(),然后是print (s['POSITIVE'])
  • 你想要正面记录吗?
  • @MAttR ,我想只显示'POSITIVE 1193'

标签: python pandas numpy count pandas-groupby


【解决方案1】:

首先过滤你的数据框:

ltweet.query('sentiment_type == "POSITIVE"')\
      .groupby('sentiment_type')['sentiment_neutral'].count()

【讨论】:

    【解决方案2】:

    你试过了吗?

    ltweet.groupby(['sentiment_type'])['sentiment_neutral'].count().loc['POSITIVE']

    来自文档: https://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.loc.html

    【讨论】:

    • 是的,它只显示了我的价值(没有显示正面)。我喜欢看像 POSITIVE 1193 之类的东西
    • ltweet.groupby(['sentiment_type'])['sentiment_neutral'].count().loc[['POSITIVE']]
    【解决方案3】:

    这是另一种方法。不用group by,你可以只过滤掉积极的情绪,然后计算行数

    cnt_pos =ltweet[ltweet["sentiment_type"]=="POSITIVE"]["sentiment_type"].count()
    

    用于打印目的

    print("POSITIVE {0}".format(cnt_pos))
    

    【讨论】:

      猜你喜欢
      • 2018-06-08
      • 2017-01-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-19
      • 2017-09-23
      • 2022-11-25
      相关资源
      最近更新 更多