LdM
LdM

Reputation: 704

How to iterate code over each string in a list of strings in pandas

I have a list of objects that I would like to check and extend. Specifically, my initial dataframe is

Name        
1   object1 
7   object2 
6   object3 
4   object4 
0   object5 
8   object6 

I apply the following function to my dataframe:

def example(file):
    
...
    my_list2 = []
    for u in file['Name']:

        try:
            minim = ...
        except:
            minim = 'NA'
        my_list2.append(minim)

    file['Others'] = my_list2
    return file

to create a new column, Others, that looks like:

    Name         Others
    1   object1 [object4, object3, object22]
    7   object2 [object1]
    6   object3 [object1]
...

What I would like to do is to apply the function also to each element in the lists in Others column. This means that I should run it for each variable in the first list, i.e.,

[object4, object3, object22]

in order to add to my initial dataframe one row (object22, since it is not included in Name column) and have something like this

    Name         Others
    1   object1 [object4, object3, object22]
    7   object2 [object1]
    6   object3 [object1]
...
   10   object22 [object23, object40, object1]

Of course, the loop stops to run when all the elements in Others are in the Name column. objectX is just a dummy name for this example: the name of variables in Name could be dog, or mom, or whatever else.

I do not know how to run the function in order to apply it to the elements in the lists under the Others column, to append the results in the original dataset.

Please let me know if you need more information.

Summary:

I need to run the function

Upvotes: 0

Views: 90

Answers (1)

Ferris
Ferris

Reputation: 5601

This answer is only to solve how to extract the new Name to handle.

you can use 'explode' to turn the list's elements to rows:

data = [{'Name': 'object1', 'Others': ['object4', 'object3', 'object22']},
 {'Name': 'object2', 'Others': ['object1']},
 {'Name': 'object3', 'Others': ['object1']}]
df = pd.DataFrame(data)
print(df)

          Name                        Others
    0  object1  [object4, object3, object22]
    1  object2                     [object1]
    2  object3                     [object1]

df_exp = df.explode('Others')
print(df_exp)

          Name    Others
    0  object1   object4
    0  object1   object3
    0  object1  object22
    1  object2   object1
    2  object3   object1

objn = pd.Series(list(set(df_exp['Others']) - set(df['Name'])), name='Name')
print(objn)

    0     object4
    1    object22
    Name: Name, dtype: object

then apply func to objn create Others content. Denote the new dataframe as dfn.

df = df.append(dfn)

repeat the explode and apply func until len(objn) == 0.

Upvotes: 1

Related Questions