Math142
Math142

Reputation: 19

Compare values in a Dictionary and in a dataframe

I want to loop over every key and every value of each and compare the value with a specific column of a data frame, however, I can only compare with the index of the value not the real data

{1: [1, 2, 3, 4, 5, 6, 7], 2: [1, 2, 3, 6, 7], 3: [1, 2, 3, 4, 5, 6, 7]}

here is the data frame that I am talking about.

   id  course_id  weight
0   1          1      10
1   2          1      40
2   3          1      50
3   4          2      40
4   5          2      60
5   6          3      90
6   7          3      10

I want o be able to compare,for example, the value 1 in the list at each key with the id in my data frame. If you can explain the algorithm, I will be able to make it work after that. I tried to find an answer but I have difficulty to apply what I saw.

Upvotes: 0

Views: 127

Answers (1)

Cornelius Roemer
Cornelius Roemer

Reputation: 7929

This is how you check whether all the values of the series 'id' are equal to the values of the dict entry '1':

pd.Series(df['id'] == d[1]).all()  # True

If you want to see which entries differ, you can simply do:

df['id'] == d[1]  # pd.Series([True,True,True,True,True,True,True])

d here is your dictionary and df your dataframe.

Upvotes: 1

Related Questions