Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why am I getting an error saying unhashable list?

This is the code that I’m running on PyCharm and keep getting an error saying if form in exceptions:
TypeError: unhashable type: ‘list’. I am new to python. Kindly help me out
enter image description here

        import matplotlib.pyplot as plt
        import pandas as pd
        import nltk
        
        data=pd.read_excel(r"C:\Users\amgup\Downloads\classification\Model_Dataset.xlsx", usecols=['Category','Title','Description'])
        
        # Combining title and description into one column
        data['combined']= (data['Title']+' '+data['Description']).str.lower()
        data=data.drop(columns=['Title','Description'])
        print(data)
        
        # shuffling the order of the rows. sample() returns random rows from the data and frac specifies what fraction has to be returned
        # frac=1 means the entire data in a random order
        data=data.sample(frac=1)
        print(data)
        
        # Counting the numnber of times each category has appeared
        count=data['Category'].value_counts()
        print(count)
        
        # Creating a barplot of the count
        bar_plot=plt.barh(count.keys(),count.values)
        plt.savefig(r"C:\Users\amgup\Downloads\classification\barplot_of_categories.png")
        
        y=list(data['combined'])
        print(y)
        
        #lemmatization
        from nltk.corpus import stopwords
        nltk.download('stopwords')
        stop_words = set(stopwords.words('english'))
        tokens=[]
        from nltk.stem import WordNetLemmatizer
        nltk.download('wordnet')
        lemmatizer = WordNetLemmatizer()
        for x in y:
            x = str(x)
            x = x.lower()
            x = x.split()
            x = [lemmatizer.lemmatize(x) for word in x if word not in stop_words]
            x = ' '.join(x)
            tokens.append(x)
        print(tokens)

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

x = x.split() turns your string x into the list.

In your comprehension, you should put lemmatizer.lemmatize(word) instead of lemmatize(x)

You are looping through the words in x x and you want to do transformation on each word

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading