Home > Enterprise >  Python function that adds nested list to value
Python function that adds nested list to value

Time:02-18

I'm trying to write a function that adds a list of lists to a key in a dictionary based on a condition. The dataframe looks similar to the following:

labels        TEXT
 -1    this is a sentence...
 -1    this is a sentence...
  1    this is a sentence...
  2    this is a sentence...
  2    this is a sentence...
  3    this is a sentence...
  3    this is a sentence...

I want every key in the dictionary to have values associated with them based on the df.labels I have.

Is there a way to append the string to the values of a dictionary? This is the code I have so far.

def function(df):
    
    my_nums = list(set(df.labels.tolist()))
    
    my_dict = {}
    for i in range(len(my_nums)):
        for k in range(len(df)):
            if my_nums[i] == df.labels[k]:
                # add the list of lists to a key in the dictionary
                # my_dict[label_nums[i]] = df.TEXT[k] doesn't quite work.
           
    return my_dict

CodePudding user response:

You could groupby, construct lists (this creates a Series that maps labels to lists) and use to_dict method to construct a dictionary:

out = df.groupby('labels')['TEXT'].agg(list).to_dict()

Output:

{-1: ['this is a sentence...', 'this is a sentence...'],
 1: ['this is a sentence...'],
 2: ['this is a sentence...', 'this is a sentence...'],
 3: ['this is a sentence...', 'this is a sentence...']}
  • Related