Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pythonic way to convert a dictionary to a numpy array

This is more of a question about programming style. I scrap webpages for fields such as: "Temperature: 51 - 62", "Height: 1000-1500"...etc The results are saved in a dictionary

{"temperature": "51-62", "height":"1000-1500" ...... }

All key and values are string type. Every key can map to one of many possible values. Now I want to convert this dictionary to numpy array/vector. I have the following concerns:

  • Each key corresponds to one index position in the array.
  • Each possible string value is mapped to one integer.
  • For some dictionary, some keys are not available. For example, I also have a dictionary that has no "temperature" key, because that webpage doesn't contain such field.

I am wondering what is the most clear and efficient way of write such a conversion in Python. I am thinking of building another dictionary maps the key to the index number of the vector. And many other dictionaries that maps the values to integers.

Another problem I am having is I am not sure about the range of some keys. I want to dynamically keep track of the mapping between string values and integers. For example, I may find that key1 can map to a val1_8 in the future.

Thanks

like image 740
fast tooth Avatar asked Jul 30 '26 12:07

fast tooth


1 Answers

Try a pandas Series, it was built for this.

import pandas as pd
s = pd.Series({'a':1, 'b':2, 'c':3})
s.values # a numpy array
like image 200
U2EF1 Avatar answered Aug 01 '26 03:08

U2EF1



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!