I have a Pandas DataFrame of 23 columns and 1119 rows.
Here is the issue, columns 13, 14, 20 and 21 are of float dtype.
If data in column 13 and 14 is nan
, then they are present in 20 and 21, and vice versa.
I want to create a column, if value is missing, get from the other.
Example: column 13 and 14 is nan
then get value from 20 and 21.
Here is what I came up with, I created a function and iterated using itertuples
def AP_calc(df):
for i in df.itertuples():
if i[20]==np.nan & i[21]==np.nan:
pool = i[13] + i[14]
else:
pool = i[20] + i[21]
return pool
then used an apply function but this does not work.
df["test"] = df[['AP in %','AP_M in %','FixP in €','FixP C in €']].apply(AP_calc,axis=1)
I have tried other methods too but not working, please help me out, please