Python: how to get values ​​from a dictionary from pandas series

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

So, here I am trying to find an answer to the same question.

Select a 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 appropriate type_dict value, but the following was the only thing I could think of and didn't work:

type_dict[df.type]

      

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

type_dict[df.type.values]

      

TypeError: unhashable type: 'numpy.ndarray'

Updated question:

for pandas DataFrame let's say 'df' how can I plot speed over meters with type as marker dictionary key.

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)) 

      

scatter plot doesn't work for me ...

+3


source to share


2 answers


Pass dict as argument 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 search for the key value in the dict and return the associated value from the dict.

+6


source


In pandas this should work



df['val'] = df.apply(lambda x: type_dict[x['type']], axis=1)

      

+3


source







All Articles