Reputation: 159
I am curious if it is possible to multiply two columns A,B
by only non NAN values.
I have the following dataframe with the expected results:
A B C
Date Symbol
1/1/2017 BTC Nan 2 Nan
ETH 3 Nan 6
XRP 2 Nan 4
1/2/2017 BTC Nan 3 Nan
ETH 1 Nan 3
XRP 2 Nan 6
I am trying to multiply non Nan values of column A
by non-NaN values of Column B
and assign the result to column C
.
I want to iterate over the dataframe. I have tried few things, but nothing is working.
Upvotes: 2
Views: 5022
Reputation: 1
df = pd.DataFrame(data=obj)
df['t'] = (df['col1'].ffill())* (df['col2'].ffill())```
col1 col2 t
0 1.0 3.0 3.0
1 2.0 4.0 8.0
2 1.0 NaN 4.0
3 NaN 8.0 8.0
Upvotes: 0
Reputation: 323286
Check with ffill
#df=df.replace('Nan',np.nan)# Nan is not NaN , replace it
#df=df.apply(pd.to_numeric,1) # convert to numeric
df.C=df.A*(df.B.ffill())
df
Out[130]:
A B C
Date Symbol
1/1/2017 BTC NaN 2.0 NaN
ETH 3.0 NaN 6.0
XRP 2.0 NaN 4.0
1/2/2017 BTC NaN 3.0 NaN
ETH 1.0 NaN 3.0
XRP 2.0 NaN 6.0
Upvotes: 4