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

+2

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

libin

10 Answers

New Answer

+3

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.

+2

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

+1

fillna will affect everything. Try the following, instead: df.loc[df['a']==0, 'b'] = 0 https://code.sololearn.com/cUu3pQq9LdME/?ref=app

0

A and B are columns of a dataframe

0

Thank you Kiibo for replying

0

Thank you Kuba

0

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

0

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

0

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

-1

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