Search code examples
pythonpython-3.xpandaspandas-loc

Error: '>' not supported between instances of 'method' and 'int'


enter image description here

This code isn't working in Jupyter IDE. I can't find my mistake. Please help.

The first 5 rows of the dataframe I am using is shown here: enter image description here


Solution

  • Try the following:

    df.loc[df['compound'] > 0,'SentimentType'] = 'Positive'
    df.loc[df['compound'] < 0,'SentimentType'] = 'Negative'
    df.loc[df['compound'] == 0,'SentimentType'] = 'Neutral'
    

    Instead of retrieving the column through df.compound, you should do df['compound']. You can also tell from the error message that you received that df.compound is a method name and not the column that you are looking for.