Skip to content
Advertisement

How to clean survey data in pandas

Input:

enter image description here Output:

enter image description here

here’s the data:

d = {'Morning': ["Didn't answer", "Didn't answer", "Didn't answer", 'Morning', "Didn't answer"], 'Afternoon': ["Didn't answer", 'Afternoon', "Didn't answer", 'Afternoon', "Didn't answer"], 'Night': ["Didn't answer", 'Night', "Didn't answer", 'Night', 'Night'], 'Sporadic': ["Didn't answer", "Didn't answer", 'Sporadic', "Didn't answer", "Didn't answer"], 'Constant': ["Didn't answer", "Didn't answer", "Didn't answer", 'Constant', "Didn't answer"]}

JavaScript

I want the output to be:

d = {"Time of the day": ["Didn't answer", "['Afternoon', 'Night']", "Sporadic", "['Morning', 'Afternoon', 'Night', 'Constant']", "Night"]}

JavaScript

so if there’s no answer in every column in a row, the value in the new data frame would be “Didn’t answer” and if there’s at least one answer like “night”, the value in the new data frame would be “night” and if there are multiple answers like ” Morning”, “Night” the value in the new data frame would be a list of the answers

Advertisement

Answer

You can use:

JavaScript

Output:

JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement