0

I try to find a solution, but I can't :(

I have a DF where one of the columns is a "child" and another is "parent".

I need to check if the parent exists in a "child" column as a child itself, If not I want to create a new row where the value from the "parent" field will be entered as a "child" value and all the other values will be entered manually.

a_b_c - child; a_b - parent

a_b - child; a - parent

if a_b exists, do nothing and check next row, if not - create "a_b" child with "a" parent

Thank you soo much :)

Dishin H Goyani
  • 7,195
  • 3
  • 26
  • 37
  • please provide proper sample data. – Dishin H Goyani Jun 24 '20 at 11:59
  • 1
    Welcome to StackOverflow. Please include a small sample of your data along with your desired results. Take a look at [how-to-make-good-reproducible-pandas-examples](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples). – Shubham Sharma Jun 24 '20 at 12:07

1 Answers1

0

I treated your example as a sample dataframe

   child parent
0  a_b_c    a_b
1    a_b      a

The following code extracts the rows in which the parent is not there in the child col

child_exists = df.parent.apply(lambda x: x in df.child.tolist())
df[~child_exists].parent # This will return the parents not present in the child col
bh00t
  • 71
  • 6