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?
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.
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.
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.
A pandas DataFrame or Series can be hashed using the pandas.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With