0

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

  • for the objects in the column name. This generates a list of strings per each object (eventually, empty lists too);
  • for each object in the list of strings: this creates new rows in Name column
  • for the new objects added in the Name column
3
  • 3
    maybe you can provide a minimal-reproducible-example with your question. In fact, I can't get what you really wanted clearly in the question. Commented Jan 22, 2021 at 3:34
  • 1
    @Ferris I updated the question. I do not know how to reduce the code in my function in order to make the example reproducible. You can think of the function as one which generates randomly a list of 'object' variables with suffix from 1 to 100 (just for make it simpler; but the variables in Name can be 'mom', 'dog', 'house', ...). Please let me know if you need more information. Thanks a lot Commented Jan 22, 2021 at 13:37
  • 1
    I post an answer for it, hope it will help. Commented Jan 23, 2021 at 2:02

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.