Reputation:
let us suppose that in my dataframe, we have following categorical data [3,4,5,6,7,8,9], i want to map values [3,4,5,6,7] to the zero and [8,9] to the one, of course i can use np.where function, but i have tried dictionary method like this
winequality["quality"] =winequality["quality"].map({[3, 4, 5, 6, 7]:0,[8, 9]:1})
where winequality is dataframe and quality is column, what i have got is this error
TypeError: unhashable type: 'list'
i have tried such kind of mapping and it was working fine, so exactly i dont remember which part i am missing, could you help me please?
Upvotes: 0
Views: 290
Reputation: 71707
>>> winequality
quality
0 3
1 4
2 5
3 6
4 7
5 7
6 8
7 9
8 9
9 8
Series.map
Instead of list
we can create a dictionary with tuple
as its keys, then flatten the dictionary, and map
the values
dct = {(3, 4, 5, 6, 7):0, (8, 9): 1}
dct = {i:v for k, v in dct.items() for i in k}
winequality['quality'].map(dct)
Series.replace
replace
can be used without requiring to flatten
the dictionary first.
dct = {(3, 4, 5, 6, 7):0, (8, 9): 1}
winequality['quality'].replace(dct)
0 0
1 0
2 0
3 0
4 0
5 0
6 1
7 1
8 1
9 1
Name: quality, dtype: int64
Upvotes: 4
Reputation: 2300
I believe the part where you try to create the dictionary is wrong: {[3, 4, 5, 6, 7]:0,[8, 9]:1}
. You should replace this with
d1 = {k: 0 for k in [3,4,5,6,7]}
d2 = {k: 1 for k in [8,9]}
d = {**d1, **d2}
That would give you
{1: 0, 3: 0, 4: 0, 9: 1, 10: 1}
for d
, which you can then use in .map
.
Upvotes: 2
Reputation: 41437
If you want to map()
it, the dict
keys need to be enumerated, not as a list, e.g.:
zeros = {key: 0 for key in [3, 4, 5, 6, 7]}
ones = {key: 1 for key in [8, 9]}
winequality["quality"] = winequality["quality"].map({**zeros, **ones})
Upvotes: 3