[英]Select particular row from a DataFrame based on value in a column in pandas
I am doing sentiment Analysis project. 我正在做情绪分析项目。 I got the results. 我得到了结果。 I counted the number of positive, neutral and negative. 我数了积极,中立和消极的人数。
Here's the code 这是代码
ltweet.groupby(['sentiment_type'])['sentiment_neutral'].count()
# ltweet= the name of object
# sentiment_type= column
#sentiment_neutral= column
Here;s the output 这是输出
Out[105]:
sentiment_type
NEGATIVE 280
NEUTRAL 1308
POSITIVE 1193
Name: sentiment_neutral, dtype: int64
I only want to display the 'POSITIVE' only (not negative and neutral) 我只想显示“正”(不是负数和中性)
I tried other codes but still get an error. 我尝试了其他代码,但仍然出现错误。 Can anyone help me with this? 谁能帮我这个?
Filter your dataframe first: 首先过滤您的数据框:
ltweet.query('sentiment_type == "POSITIVE"')\
.groupby('sentiment_type')['sentiment_neutral'].count()
Have you tried this? 你有尝试过吗?
ltweet.groupby(['sentiment_type'])['sentiment_neutral'].count().loc['POSITIVE']
From the documentation: https://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.loc.html 从文档中获取: https : //pandas.pydata.org/pandas-docs/stable/genic/pandas.Series.loc.html
Here is an alternative way of doing it. 这是一种替代方法。 Instead of using a group by, you can filter out only the positive sentiments and then count the number of rows 您可以只过滤出正面情绪,然后计算行数,而不是使用分组依据
cnt_pos =ltweet[ltweet["sentiment_type"]=="POSITIVE"]["sentiment_type"].count()
For printing purposes 用于打印
print("POSITIVE {0}".format(cnt_pos))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.