I am editing my previous question as it was flawed. I have a data frame named df. In that data frame, columns contain values, some of them are negative values, zeros, and NaN. I want to replace these values and store a respective value of the flag in another data frame at the respective index.
df = pd.read_excel('Check.xlsx')
df_ph_temp = df.iloc[:,2:5]
df_flags = pd.DataFrame(index=df.index, columns=df.columns)
flag_ph_temp = df_flags.iloc[:,2:5]
for rowIndex, row in df_ph_temp.iterrows() :
for colIndex, value in row.items() :
if value == 0 :
df_ph_temp.loc[rowIndex, colIndex] = df_ph_temp.loc[rowIndex - 1, colIndex]
flag_ph_temp.loc[rowIndex, colIndex] = 1
elif value < 0 :
df_ph_temp.loc[rowIndex, colIndex] = 0
flag_ph_temp.loc[rowIndex, colIndex] = 1
elif value > 200 :
df_ph_temp.loc[rowIndex, colIndex] = 130
flag_ph_temp.loc[rowIndex, colIndex] = 2
elif value == np.nan : # Not working... Why?
df_ph_temp.loc[rowIndex, colIndex] = df_ph_temp.loc[rowIndex - 1, colIndex]
flag_ph_temp.loc[rowIndex, colIndex] = 1
else :
continue
I am not getting any errors but also not getting desired output. Replacing NaN values and storing the resp. flag values in the flag's data frame, this part of the program is not working. I think this is because data contains more than 2 lines with NaN values. Is there a way to fix this? I tried
df_ph_temp[colIndex].fillna(method ='ffill', inplace = True)
before the if condition but still not able to achieve desired results.
I am unable to figure it out. Kindly help.