0

I have two list like

l1 = [np.nan, 54, 40, 71, 20, 27, 33, 38, 104, 36, 13]
l2 = [64,     50, 81, 30, 37, 43, 48, 204, 46, 23, np.nan]

how can I do an elementwise multiplication ignoring the nans.

So to return

res = [ nan, 2700, 3240, 2130, 740, 1161, 1584, ......, nan]

I found one way of doing it via pandas

res = pd.Series(l1).multiply(pd.Series(l2), fill_value = np.nan)

but can this be done without converting it to pd

ThatQuantDude
  • 759
  • 1
  • 9
  • 26
  • 1
    `res = [a*b for a,b in zip(l1, l2)]`? – mozway May 06 '22 at 12:03
  • What do you mean by ignoring the nans? Your example output seems to not ignore them in any way. nan * 64 = nan, 54 * 50 = 2700 and so on. It looks like you did "regular" elementwise multiplication. – Lomtrur May 06 '22 at 12:04

0 Answers0