【发布时间】:2018-03-07 22:24:55
【问题描述】:
这个问题已经在 SO 上被问过很多次了,但在每种情况下,我发现答案总是改变 OP 的代码,以不同的方式获得正确的结果。 ExampleExample
您如何将数据框列转换为可以散列的东西,而不是使用不同的方法来解决问题?或者,如果这不可能,为什么还要使用其他方法?
import pandas as pd
d = {'name':['bil','bil','bil','jim'],
'col2': ['acct','law', 'acct2','law'],
'col3': [1,2,3,55],
'col4': [1,1,1,2]
}
df2 = pd.DataFrame(data=d)
coursesFilter=['acct']
print(df2[df2['col2'].isin([coursesFilter])]) #TypeError: unhashable type: 'list'
print(df2[df2['col2'].isin([pd.Series(coursesFilter)])]) #TypeError: 'Series' objects are mutable, thus they cannot be hashed
print(df2[df2['col2'].isin([pd.Series(coursesFilter).tolist()])]) #TypeError: unhashable type: 'list'
【问题讨论】:
标签: python python-3.x pandas