Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Type error: unhashable type 'list' while selecting subset from specific columns pandas dataframe

I have a dataframe that has about 20 columns and I am trying to get a subset of the dataframe by selecting only some specific columns about 6. My line of code is:

df3_query = df3[['Cont NUMBER'],['PL NUMBER'],['NAME'],['LOAN COUNT'],['SCORE    MINIMUM'],['COUNT PERCENT']]

I am getting an error as

TypeError: unhashable type: 'list'

May I know the reason in which why I get this error? Also I would like to select only those columns from the df3 dataframe. Can anyone help me on this?

like image 777
User1090 Avatar asked Jan 21 '16 12:01

User1090


People also ask

How do I fix TypeError Unhashable type list?

This error occurs when trying to hash a list, which is an unhashable object. For example, using a list as a key in a Python dictionary will cause this error since dictionaries only accept hashable data types as a key. The standard way to solve this issue is to cast a list to a tuple, which is a hashable data type.

What does Unhashable mean in Python?

TypeError: unhashable type: 'list' usually means that you are trying to use a list as an hash argument. This means that when you try to hash an unhashable object it will result an error. For ex. when you use a list as a key in the dictionary , this cannot be done because lists can't be hashed.

What does .values in pandas do?

The values property is used to get a Numpy representation of the DataFrame. Only the values in the DataFrame will be returned, the axes labels will be removed. The values of the DataFrame. A DataFrame where all columns are the same type (e.g., int64) results in an array of the same type.

Is a DataFrame hashable?

A pandas DataFrame or Series can be hashed using the pandas.


1 Answers

You need to write your column names in one list not as list of lists:

df3_query = df3[['Cont NUMBER', 'PL NUMBER', 'NAME', 'LOAN COUNT', 'SCORE    MINIMUM', 'COUNT PERCENT']]

From docs:

You can pass a list of columns to [] to select columns in that order. If a column is not contained in the DataFrame, an exception will be raised. Multiple columns can also be set in this manner

like image 187
Anton Protopopov Avatar answered Nov 14 '22 21:11

Anton Protopopov