简体   繁体   English

根据熊猫列中的值从DataFrame中选择特定的行

[英]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.

相关问题 如何基于搜索特定列中的数据在 Pandas Dataframe 中选择一行 - How to select a row in Pandas Dataframe based on searching a data in a particular column 当同一行中的另一列为NaN时,如何从熊猫数据框中选择特定的列值? - How to select a particular column value from a pandas dataframe when another column in the same row is NaN? 根据Pandas中第二列的条件,用另一行的同一列的值填充特定行的列中的值 - Fill values in a column of a particular row with the value of same column from another row based on a condition on second column in Pandas 根据列值删除Pandas中的DataFrame行 - Deleting DataFrame row in Pandas based on column value 根据值选择pandas dataframe列 - Select column of pandas dataframe based on value 在 DataFrame 中选择特定的行和列 - To select particular row and column in DataFrame Pandas数据框根据查询数据框中的值选择行,然后根据列值选择其他条件 - Pandas Dataframe Select rows based on values from a lookup dataframe and then another condition based on column value 如何根据不同的条件为 pandas dataframe 中的特定列赋值? - How to assign value to particular column in pandas dataframe based on different conditions? 根据不同的列值选择Pandas Dataframe列值 - Select Pandas Dataframe Column Value Based on Different Column Value 根据两列的函数在 Pandas 数据框中选择列值最大的行 - Select row in Pandas dataframe where a column value is max based on a function of two columns
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM