pythonpandasdataframeword-cloud

KeyError: False; Pandas dataframe error when creating list from dataframe columns


I am trying to create lists out of a dataframe column in pandas. My code is:

positive = tweet_df[tweet_df['updated sentiments'].equals('positive')]

This gives me the error: enter image description here

The dataframe for this code is: enter image description here

I have even tried using the keywords extend and append to add the appropriate data to the lists, but I am not sure why it is not checking the 'updated sentiments' column to populate the list:

pos_words = []
neg_words = []
net_words = []

for i in range(len(tweet_df['updated sentiments'])):
  if tweet_df['updated sentiments'].equals('negative'):
    neg_words.extend(tweet_df['Tweet Body'].tolist())
  elif tweet_df['updated sentiments'].equals('positive'):
    pos_words.extend(tweet_df['Tweet Body'].tolist())
  else:
    net_words.extend(tweet_df['Tweet Body'].tolist())

Solution

  • I dont think equals is the right function here. equals function compares two series or dataframes to check whether they are equal.

    Instead you can use the loc function.

    positive = tweet_df.loc[tweet_df['updated_sentiments']=='positive', 'Tweet Body'].tolist()