简体   繁体   中英

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

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))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM