Timeline for How to fill an missing values in a column based on another column
Current License: CC BY-SA 4.0
16 events
when toggle format | what | by | license | comment | |
---|---|---|---|---|---|
Mar 10, 2019 at 13:07 | history | edited | Victor Oliveira | CC BY-SA 4.0 |
added 255 characters in body
|
Mar 8, 2019 at 21:53 | comment | added | Python Newbie | It works! Thanks a lot! | |
Mar 8, 2019 at 17:50 | comment | added | Victor Oliveira |
you cpy & paste ir wrong: shoes[shoes.Comment.isnull()].merge(df_shoes,on=['Brand'], how='left', suffixes=('', '_notnull')) shoes.Comment.fillna(value=temp.Comment_notnull) They are two commands. Try now, please.
|
|
Mar 8, 2019 at 17:41 | comment | added | Python Newbie |
Oh no that's completely fine. I quite understand how challenging it can be. I tried out the code but I got an error message of SyntaxError: can't assign to operator Just came across a similar question on stackoverflow that may work. I can send you the link if you're interested
|
|
Mar 8, 2019 at 17:29 | comment | added | Victor Oliveira | Ok then. I think this will work: - temp = shoes[shoes.Comment.isnull()].merge(df_shoes, on=['Brand'], how='left', suffixes=('', '_notnull')) - shoes['Comment'] = shoes.Comment.fillna(value=temp.Comment_notnull) Let me know if this works. Sorry for not getting a cleanest answered. But I cant think to do it in another way. | |
Mar 8, 2019 at 17:22 | comment | added | Python Newbie | I don't know if that makes sense. Maybe I'm just not explaining it well haha | |
Mar 8, 2019 at 17:22 | comment | added | Python Newbie | So basically, the shoes dataframe is a (6000,6) dataframe. however, the comment column contains about 500 missing values. So what I'm planning to do is to fill this missing data with the most common comment for each brand (i.e the df_shoes data which was obtained from taking the mode of the comment section of the shoes dataframe). | |
Mar 8, 2019 at 17:13 | comment | added | Victor Oliveira | Humm, you could try: shoes['Comment'] = shoes.Comment.fillna(value=df_shoes.Comment) shoes = shoes.sort_values(by='Brand').fillna(method='ffill') But it is werid that you have colums with same brand and different comments. Basically in the approach above we use df_shoes.Comment colum to fill null values, then we sort by brand and use forward fill, but check if it is normal to have different labels for same brand. | |
Mar 8, 2019 at 17:02 | comment | added | Python Newbie | Basically, What I'm trying to do is to assign comments present in the df_mode data frame to missing comments in the shoes dataframe without having to create a new column | |
Mar 8, 2019 at 17:00 | comment | added | Python Newbie | Thanks again for your response. But it suggests all the elements in the comment column are empty which isn't the case. I think that's a mistake on my part. I'll edit the question to reflect that. | |
Mar 8, 2019 at 16:50 | comment | added | Victor Oliveira | I edited again the answer | |
Mar 8, 2019 at 16:49 | history | edited | Victor Oliveira | CC BY-SA 4.0 |
Added a more detailed explanation
|
Mar 8, 2019 at 16:35 | comment | added | Victor Oliveira | Sorry for that, you should merge based on Brand only, try that. | |
Mar 8, 2019 at 16:35 | history | edited | Victor Oliveira | CC BY-SA 4.0 |
I was stupid, used 'Comment' also to merge, but the correct approach is to merge on Brand only.
|
Mar 8, 2019 at 16:33 | comment | added | Python Newbie | tried this but the NaN values still seem be to present | |
Mar 8, 2019 at 16:19 | history | answered | Victor Oliveira | CC BY-SA 4.0 |