I'm new to this, so this may sound weird but basically, I have a large dataframe but for simplification purposes let's assume the dataframe is this:
import pandas as pd
import numpy as np
dfn = pd.DataFrame({'a':[1,2,3,4,5],
'b':[6,7,8,9,10],
'c':np.nan})
dfn
Output:
a b c
0 1 6 NaN
1 2 7 NaN
2 3 8 NaN
3 4 9 NaN
4 5 10 NaN
What I want to do is to fill in values in the 'c' column based off of a condition, namely if the corresponding row value in 'a' is odd, then add it to the corresponding row value 'b' and input into 'c', else, just use the 'a' value for 'c'.
What I currently have is this:
for row in range(dfn.shape[0]):
if dfn.loc[row]['a']%2!=0:
dfn.loc[row]['c']=dfn.loc[row]['a']+dfn.loc[row]['b']
else:
dfn.loc[row]['c']=dfn.loc[row]['a']
dfn
Output:
a b c
0 1 6 NaN
1 2 7 NaN
2 3 8 NaN
3 4 9 NaN
4 5 10 NaN
Nothing seems to happen here and I'm not entirely sure why.
I've also tried a different approach of:
is_odd=dfn[dfn['a']%2!=0]
is_odd['c'] = is_odd['a'] + is+odd['b']
is_odd
Here, weirdly I get the right output:
a b c
0 1 1 2
2 3 3 6
4 5 5 10
But when I call dfn again, it comes out with all NaN values.
I've also tried doing it without using a variable name and nothing happens.
Any idea what I'm missing or if there's a way of doing this?
Thanks!