假设我有一个数据框和一个像这样的字典:
df = pd.DataFrame({"letters": ["a","b","c","b","c"], "numbers": [1,2,3,1,2]})
correct_mapping={"a":1,"b":2,"c":3}
我要过滤没有正确映射的行。
我尝试了这个,但是失败了:
df[correct_mapping[df["letters"]] != df["numbers"]]
TypeError: 'Series' objects are mutable, thus they cannot be hashed
任何帮助将不胜感激。
预期输出:
letters numbers
3 b 1
4 c 2
答案 0 :(得分:2)
与map
比较:
df[df['letters'].map(correct_mapping) != df['numbers']]
letters numbers
3 b 1
4 c 2