Python 类型错误:从特定列熊猫数据帧中选择子集时不可散列的类型“列表”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34923728/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Type error: unhashable type 'list' while selecting subset from specific columns pandas dataframe
提问by User1090
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:
我有一个大约有 20 列的数据框,我试图通过只选择一些大约 6 的特定列来获取数据框的一个子集。我的代码行是:
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?
我可以知道我收到此错误的原因吗?另外我想只从 df3 数据框中选择那些列。谁可以帮我这个事?
采纳答案by Anton Protopopov
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
您可以将列列表传递
[]
给以按该顺序选择列。如果 DataFrame 中不包含列,则会引发异常。也可以用这种方式设置多列