Append to dictionary based on list values

user58653

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:

for i in label_list:
 if i == 0:
   dict.update({i:"Zero"})
 elif i == 1:
   dict.update({i:"One"})
 elif i == 2:
   dict.update({i:"Two"})
 elif i == 3:
   dict.update({i:"Three"})
 else:
   dict.update({i:"Four"})

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

Mark

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

import pandas as pd

words = {
    0: 'zero',
    1: 'one',
    2: 'two',
    3: 'three',
    4: 'four'
}

l = [0, 2, 3, 1, 4, 0, 1, 2]

nums = pd.Series(l)
pd.DataFrame({'n': nums, 'words':nums.map(words)})

Which creates the dataframe:

    n   words
0   0   zero
1   2   two
2   3   three
3   1   one
4   4   four
5   0   zero
6   1   one
7   2   two

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Append unequal length of values from a list to a dictionary

How to append values to a list in a dictionary while using a while loop?

Convert dictionary into list with length based on values

append list values to a dictionary

Append string to specific values in a list of dictionary - Python

Group the list of dictionary as list of values based on key

How can I append values from a JSON dictionary to a new list?

Append all key values to list that are present in value list of a dictionary

Unable to append a list of values to specific keys of a dictionary

Split/Sort nested list into a dictionary based on values

compare 2 lists and append values to new list of dictionary

Convert a list of tuples to a dictionary, based on tuple values

How to sort a list based on dictionary values?

Extracting values as a dictionary from dataframe based on list

Creating new matrix based on dictionary values and a list

Add to values in a list inside a dictionary with append

Map a list as dictionary values, based on an already defined dictionary/order

How to append list values to list of dictionary with the same length?

how to avoid for loop to append dictionary values in a list

Changing dictionary values based on matches between dictionary keys and a list

Map values in a List based on custom Dictionary - Python

How to find and append dictionary values which are present in a list

How to get values of list dictionary and append it to a string?

Sort dictionary based on values from a list

How to filter a dictionary based on the values of a list

Updating dictionary values based on another list of dictionaries

How to append Dictionary keys null values in a list?

Converting elements of a list based on corresponding dictionary values

filter list of dictionary based on two values