Python help please | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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

28th May 2020, 10:28 AM
libin
libin - avatar
8 Answers
+ 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.
28th May 2020, 10:43 AM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
+ 2
Kuba Siekierzyński Thank you so much. So kind of you. I really appreciate.
28th May 2020, 11:24 AM
libin
libin - avatar
+ 1
fillna will affect everything. Try the following, instead: df.loc[df['a']==0, 'b'] = 0 https://code.sololearn.com/cUu3pQq9LdME/?ref=app
28th May 2020, 11:21 AM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
0
A and B are columns of a dataframe
28th May 2020, 10:44 AM
libin
libin - avatar
0
Thank you Kiibo for replying
28th May 2020, 10:45 AM
libin
libin - avatar
0
Thank you Kuba
28th May 2020, 10:45 AM
libin
libin - avatar
0
𝐊𝐢𝐢𝐛𝐨 𝐆𝐡𝐚𝐲𝐚𝐥 I tried like this if df['a'] ==0: df['b'].fillna(0,inplace=True)
28th May 2020, 10:52 AM
libin
libin - avatar
0
Kuba Siekierzyński a and b are columns of a same dataframe df
28th May 2020, 10:58 AM
libin
libin - avatar