7

I am very new to python and trying to get value from dictionary where keys are defined in a dataframe column (pandas). I searched quite a bit and the closest thing is a question in the link below, but it doesnt come with an answer.

So, here I am trying to find answer for the same type of question.

Select from dictionary using pandas series

I have a dictionary

type_dict = {3: 'foo', 4:'bar',5:'foobar', 6:'foobarbar'}

and a data frame with the following column:

>>> df.type
0     3
1     4
2     5
3     6
4     3
5     4
6     5
7     6
8     3

I want to create a new column containing the corresponding type_dict value, but the following was the only thing I could come up and was not working:

type_dict[df.type]

TypeError: 'Series' objects are mutable, thus they cannot be hashed

type_dict[df.type.values]

TypeError: unhashable type: 'numpy.ndarray'

Updated question:

for pandas DataFrame, say 'df', how can i plot speed over meters with type as the key of marker dictionary.

mkr_dict = {'gps': 'x', 'phone': '+', 'car': 'o'}

x = {'speed': [10, 15, 20, 18, 19], 'meters' : [122, 150, 190, 230, 300], 'type': ['phone', 'phone', 'gps', 'gps', 'car']}

df = pd.DataFrame(x)
   meters  speed   type
0     122     10  phone
1     150     15  phone
2     190     20    gps
3     230     18    gps
4     300     19    car

plt.scatter(df.meters, df.Speed, marker = df.type.map(mkr_dict)) 

the scatter plot doesn't work for me...

Community
  • 1
  • 1
Kexin Xu
  • 691
  • 3
  • 10
  • 20
  • AFAIK, plain python doesn't have the notion of 'dataframes'. If you're using a framework or library (like [pandas](http://pandas.pydata.org/)), please indicate which one. – das-g May 18 '15 at 20:54
  • I think your scatter question should be a new question, I'm not a matplotlib expert but I think you can achieve it using the answer here: http://stackoverflow.com/questions/26490817/matplotlib-scatter-plot-with-different-markers-and-colors basically this iterates over each row calling scatter and passing the x,y coord and the marker style – EdChum May 18 '15 at 22:12

2 Answers2

20

Pass the dict as an arg to map:

In [79]:

df['type'].map(type_dict)
Out[79]:
0          foo
1          bar
2       foobar
3    foobarbar
4          foo
5          bar
6       foobar
7    foobarbar
8          foo
Name: type, dtype: object

This will lookup the key value in the dict and return the associated value from the dict.

EdChum
  • 376,765
  • 198
  • 813
  • 562
  • Thanks!!!!!! so it does create a list of values from the dictionary. But I am trying to use this as markers in a scatterplot, but it failed with error: TypeError: 'Series' objects are mutable, thus they cannot be hashed plt.scatter(output.x, output.y, s=area, ,marker = output.DataPointType.map(mkr_dict)) . Any idea? – Kexin Xu May 18 '15 at 21:26
  • You'd have to explain how you're passing these values but lists and Series are mutable, generally though most of these plotting libraries interface with iterable containers such as numpy arrays and should be compatible with pandas dataframes – EdChum May 18 '15 at 21:27
  • I updated my question, can you please take another look? Thanks! – Kexin Xu May 18 '15 at 22:01
3

In pandas, this should work

df['val'] = df.apply(lambda x: type_dict[x['type']], axis=1)
Julien Spronck
  • 15,069
  • 4
  • 47
  • 55