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

Kexin Xu

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

EdChum

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Get unique values from pandas series of lists

From Dev

How to increment Python Pandas DataFrame based on key/values from a dictionary

From Dev

how to get matched items from python dictionary? when values are arrays

From Dev

Python Get N max values from dictionary

From Dev

Python Programming: how to eval values from dictionary?

From Dev

Python Programming: how to eval values from dictionary?

From Dev

How to remove values from dictionary that are not numbers in Python?

From Dev

Get duplicate values from dictionary

From Dev

get all values from dictionary

From Dev

How to get a dictionary from a json file in Python?

From Dev

how to get the items from the dictionary python

From Dev

How to get value from nested dictionary in python?

From Dev

How to get a value from a list of dictionary Python?

From Dev

New pandas dataframe column using values from python dictionary

From Dev

Get records from dictionary pandas

From Dev

How to set values of a masked Pandas Series from a different mask

From Dev

Pandas, How to subtract first row value from all values in a Series?

From Dev

How to extract values from pandas Series without index

From Dev

How to group consecutive NaN values from a Pandas Series in a set of slices?

From Dev

Python How can I get all values same name keys from dictionary

From Dev

How can I get values from dictionary for particular key using python

From Dev

How to get all values for the same key from key-value pairs in python dictionary

From Dev

python pandas get index boundaries from a series of Booleans

From Dev

How to get values from Series using column names?

From Dev

How to get dictionary values in Python

From Dev

How to get dictionary values in Python

From Dev

How to get all keys from dictionary that specified values?

From Java

How to get same values from different keys in a Dictionary?

From Dev

How to get values from dictionary in jinja when key is a variable?

Related Related

  1. 1

    Get unique values from pandas series of lists

  2. 2

    How to increment Python Pandas DataFrame based on key/values from a dictionary

  3. 3

    how to get matched items from python dictionary? when values are arrays

  4. 4

    Python Get N max values from dictionary

  5. 5

    Python Programming: how to eval values from dictionary?

  6. 6

    Python Programming: how to eval values from dictionary?

  7. 7

    How to remove values from dictionary that are not numbers in Python?

  8. 8

    Get duplicate values from dictionary

  9. 9

    get all values from dictionary

  10. 10

    How to get a dictionary from a json file in Python?

  11. 11

    how to get the items from the dictionary python

  12. 12

    How to get value from nested dictionary in python?

  13. 13

    How to get a value from a list of dictionary Python?

  14. 14

    New pandas dataframe column using values from python dictionary

  15. 15

    Get records from dictionary pandas

  16. 16

    How to set values of a masked Pandas Series from a different mask

  17. 17

    Pandas, How to subtract first row value from all values in a Series?

  18. 18

    How to extract values from pandas Series without index

  19. 19

    How to group consecutive NaN values from a Pandas Series in a set of slices?

  20. 20

    Python How can I get all values same name keys from dictionary

  21. 21

    How can I get values from dictionary for particular key using python

  22. 22

    How to get all values for the same key from key-value pairs in python dictionary

  23. 23

    python pandas get index boundaries from a series of Booleans

  24. 24

    How to get values from Series using column names?

  25. 25

    How to get dictionary values in Python

  26. 26

    How to get dictionary values in Python

  27. 27

    How to get all keys from dictionary that specified values?

  28. 28

    How to get same values from different keys in a Dictionary?

  29. 29

    How to get values from dictionary in jinja when key is a variable?

HotTag

Archive