Home > Enterprise >  Pandas Python Tuples
Pandas Python Tuples

Time:07-08

I have a dataset as shown below. How do I extract the 'positive' and 'negative' sentiment counts from the 'sentiment' column and store them in a tuple? There are 0 positive words and 4 negative words, so I need a tuple showing (0, 4). Please let me know.

pandas python sentiment dataset

CodePudding user response:

col_names = ["positive", "negative"]
tuple((df.sentiment.value_counts()   pd.Series([0,0], index=col_names)).fillna(0).astype(int)[col_names].tolist())

CodePudding user response:

You could do a value counts and store that into a tuple. For example:

df = pd.DataFrame([['restaurant','pos'],
                   ['gross','neg'],
                   ['gross','neg'], 
                   ['waiters','neutral']])

df['sentiment'].value_counts()
# neg        2
# neutral    1
# pos        1

tuple(df['sentiment'].value_counts().values)
# (2,1,1)

The lines that begin with '#' are the output of calling the previous line.

  • Related