0

I basically have a column of values that I want to check are valid. I want to make a new column that gives back true or false for validity. The values are valid when they are found in the lookup of another two columns, which comprise the key and values for a dictionary.

Here, my values are exams. An EXAM are only valid if they land at a scoring site or a DBN that can score them, for instance, if 11X000 can score Math, and this is a Math exam that landed at 11X000, it's a Valid Site.

See the screenshots, the first image is what I'm trying to replicate in Python, and the second image is the dictionary.

enter image description here

enter image description here

In my code I made a dictionary for {site:exam} and am trying to check validity for all given records.

Sites_To_Exams_Dict = Sites_To_Exams_df.groupby('DBN')['ALL EXAMS'].apply(list).to_dict()

MaterialsReport_df['Valid Site'] = np.where(MaterialsTracking_df['EXAM'] in Sites_To_Exams_Dict.get(MaterialsReport_df['Scoring Site Including X']),
                                            "Y", "N")

Unfortunately this gives the error TypeError: 'Series' objects are mutable, thus they cannot be hashed

I would appreciate any different approaches or bug fix to the code above. Thanks!

Christina Zhou
  • 767
  • 1
  • 2
  • 12
  • 2
    It's not entirely clear from your description what you're trying to do. Please see [How to create good pandas examples](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) and provide sample input and output in the text of the question, rather than as a picture – G. Anderson Jul 19 '19 at 20:34

1 Answers1

1

I believe your issue is that you are trying to use a series object (the list of exams in each cell) as a key in a dictionary when you call to_dict. Series objects (like lists) are mutable and thus cannot be used as keys in a dictionary. I would try avoiding the dictionary approach and instead check use something like .isin to check if the value is in the list of valid sites.

zackg22
  • 71
  • 4