Search code examples
pythonpandas

Boolean indexing: How to select a specific value to create new column


I have a dataframe which looks like this:

col1 col2 col3
X1   Nan  Nan
foo  bar  baz
foo  bar  baz
X2   Nan  Nan
foo  bar  baz
foo  bar  baz
X3   Nan  Nan
foo  bar  baz
foo  bar  baz

I have filtered to look like this:

m = df.notna()
print(m)
col1 col2 col3
True False False
True True True
True True True
True False False 
True True True
True True True 
True False False 
True True True 
True True True

I need to select the True value in the row with False's, and create a new column with that value. For example, my resultant df should essentially look like this:

col1 col2 col3 new
foo  bar  baz  X1
foo  bar  baz  X1
foo  bar  baz  X2
foo  bar  baz  X2
foo  bar  baz  X3
foo  bar  baz  X3

I am unsure how to accomplish this with pandas, any suggestions would help


Solution

  • One possible solution is to create the new column based on 'col1' when col2 is null, then ffill the new column and drop unwanted rows with dropna:

    df["new"] = df["col1"][df["col2"].isna()]
    df["new"] = df["new"].ffill()
    df = df.dropna()
    
      col1 col2 col3 new
    1  foo  bar  baz  X1
    2  foo  bar  baz  X1
    4  foo  bar  baz  X2
    5  foo  bar  baz  X2
    7  foo  bar  baz  X3
    8  foo  bar  baz  X3