Python help please | SoloLearn: Learn to code for FREE!


Python help please

If i have 2 columns, 'a' and 'b'. 'a' has random values and 'b' has some missing values. If i want to impute missing values of 'b' with 0 if that row has 'a' value 0, then how can I achieve that in python

5/28/2020 10:28:04 AM


10 Answers

New Answer


What is the variable type? DataFrame? Array? or are those just two lists? In any way, you should iterate through with index and assign b[index] = 0 if a[index] == 0 Or at least this is how I understood your question.


Kuba Siekierzyński Thank you so much. So kind of you. I really appreciate.


fillna will affect everything. Try the following, instead: df.loc[df['a']==0, 'b'] = 0


A and B are columns of a dataframe


Thank you Kiibo for replying


Thank you Kuba


libin Type @ ,a suggestion list will pop up, choose a user to whom you want to mention Show your atttempt so that we can help you further


𝐊𝐢𝐢𝐛𝐨 𝐆𝐡𝐚𝐲𝐚𝐥 I tried like this if df['a'] ==0: df['b'].fillna(0,inplace=True)


Kuba Siekierzyński a and b are columns of a same dataframe df


libin Show your atttempt Columns is only possible only in dataframe, arrays , are you asking about these?