Python "TypeError: unhashable type: 'slice'" for encoding categorical data
what is python used for
python from scratch
python programming examples
introduction to python
I am getting
TypeError: unhashable type: 'slice'
when executing the below code for encoding categorical data in Python. Can anyone please help?
# Importing the libraries import numpy as np import matplotlib.pyplot as plt import pandas as pd # Importing the dataset dataset = pd.read_csv('50_Startups.csv') y=dataset.iloc[:, 4] X=dataset.iloc[:, 0:4] # Encoding categorical data from sklearn.preprocessing import LabelEncoder, OneHotEncoder labelencoder_X = LabelEncoder() X[:, 3] = labelencoder_X.fit_transform(X[:, 3])
# Importing the libraries import numpy as np import matplotlib.pyplot as plt import pandas as pd # Importing the dataset # dataset = pd.read_csv('50_Startups.csv') dataset = pd.DataFrame(np.random.rand(10, 10)) y=dataset.iloc[:, 4] X=dataset.iloc[:, 0:4] # Encoding categorical data from sklearn.preprocessing import LabelEncoder, OneHotEncoder labelencoder_X = LabelEncoder() # I changed this line X.values[:, 3] = labelencoder_X.fit_transform(X.values[:, 3])
What is Python used for?, Python Crash Course, 2nd Edition: A Hands-On, Project-Based Introduction to Programming Python, SQL, Networking, CompTIA, Linux, Curriculum Built For Today's Skills.
use Values either while creating variable X or while encoding as mentioned above
# Importing the libraries import numpy as np import matplotlib.pyplot as plt import pandas as pd # Importing the dataset # dataset = pd.read_csv('50_Startups.csv') dataset = pd.DataFrame(np.random.rand(10, 10)) y=dataset.iloc[:, 4].values X=dataset.iloc[:, 0:4].values
Is Python an easy language to learn?, Automate the Boring Stuff with Python: Practical Programming for Total Beginners Looking for where to learn python? Get your questions answered. Discover where to learn python and related content.
While creating the matrix
Y vector use
It will definitely solve your problem.
Base language of python, Learn Python the Hard Way: A Very Simple Introduction to the Terrifyingly Beautiful World of Computers and Code 99% Match on Learn Programming Python. Start searching with Visymo.com.
if you use .Values while creating the matrix X and Y vectors it will fix the problem.
y=dataset.iloc[:, 4].values X=dataset.iloc[:, 0:4].values
when you use .Values it creates a Object representation of the created matrix will be returned with the axes removed. Check the below link for more information
I was getting same error (TypeError: unhashable type: 'slice') with below code:
included_cols = [2,4,10] dataset = dataset[:,included_cols] #Columns 2,4 and 10 are included.
Resolved with below code by putting iloc after dataset:
included_cols = [2,4,10] dataset = dataset.iloc[:,included_cols] #Columns 2,4 and 10 are included.
Python (programming language), Take-Away Skills: This course is a great introduction to both fundamental programming concepts and the Python programming language. By the end, you'll be >>> Python Software Foundation. The mission of the Python Software Foundation is to promote, protect, and advance the Python programming language, and to support and facilitate the growth of a diverse and international community of Python programmers. Learn more. Become a Member Donate to the PSF
Python Tutorial, Python's growth in high-income countries. You can see on Stack Overflow Trends that Python has been growing rapidly in the last few years. Learn Python, a powerful language used by sites like YouTube and Dropbox. Learn the fundamentals of programming to build web apps and manipulate data. Master Python loops to deepen your knowledge.
Learn Python 2, learnpython.org is a free interactive Python tutorial for people who want to learn Python, fast. Python is a programming language. Python can be used on a server to create web applications. Start learning Python now »
The Incredible Growth of Python, Python Bitwise Operators. Bitwise operator works on bits and performs bit by bit operation. Assume if a = 60; and b = 13; Now in the binary format their values will be 0011 1100 and 0000 1101 respectively.
- What's in the csv file? In which line you got the TypeError?
- Please add the corresponding stack trace to your question.
- Tell use about the
dataset? I suspect its 'iloc' is expecting a string column label, not 2d array like slicing. The error implies that a
slice(e.g 0:4) is being used in as dictionary key, or something like that.
- Hi, I'm curious about the reason of
TypeError: unhashable type: 'slice'?