'Insert string in one column if a value is present in another column

Using Python3, trying to replace the blank cell in col 'EWIS Comp Type' with 'CONNECTOR' if there is a value, other than 'nan', in the corresponding cell of 'Srch1'.

Oh and this cannot rewrite over the entire 'EWIS Comp Type' column as there is other data not pictured; only fill in for blanks when 'Srch1' has an entry as stated above.

Apologies if not in ideal format and/or proper terminology, new to Stackoverflow and coding.

Thanks in advance

What I am starting with:

What I am starting with

df['EWIS Comp Type'].fillna(df.Srch1, inplace=True)

df['EWIS Comp Type'].str.replace(r'P[0-9]+', 'CONNECTOR', regex=True) #would not change the PXXX to CONNECTOR

also tried,

df['EWIS Comp Type'].re.sub(r'P[0-9]+', 'CONNECTOR',str) #this errored which I had no luck researching the fix

I can get the PXXX to replicate to 'EWIS Comp Type', but cannot seem to get the PXXX to be replaced by 'CONNECTOR'.

What Im trying to have happen:

What I am trying to accomplish



Solution 1:[1]

Try:

df.loc[df["Srch1"].notna(), "EWIS Comp Type"] = "CONNECTOR"
print(df)

Prints:

  EWIS Comp Type Srch1
0      CONNECTOR  P471
1      CONNECTOR  P467
2                  NaN
3                  NaN

EDIT: To populate only values which are empty (""):

df.loc[df["Srch1"].notna() & df["EWIS Comp Type"].eq(""), "EWIS Comp Type"] = "CONNECTOR"

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1