Skip to content
Advertisement

Append to dictionary based on list values

I have a list that contains 8634 values that are either 0, 1, 2, 3 or 4. I want to create a dictionary that also has 8634 key-value pairs that are based on the value in the list. For example, while traversing through the list, if it finds a zero then the key-value pair should be 0:Zero and so fourth until it reaches the end of the list.

Here is my code:

JavaScript

The current code only produces 5 Key-Value pairs. My intention is to create a dataframe out of the result.

Advertisement

Answer

Since you are looking to make a dataframe, you can can use pandas map() with a dictionary that maps numbers to words. For example:

JavaScript

Which creates the dataframe:

JavaScript
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement