'how to delete values of cells based on the value of another cell in python?

I am a complete newbie to data analysis and have a question regarding a survey analysis with Python. The study (it’s about cultivated meat) was conducted on google forms.

Here is my problem: There is a question about weekly meat consumption (question 6) and then a question about the willingness to reduce one’s meat consumption (question 9). Now I would like to delete a person’s answer in the column of question 9, if that person answered ‚never‘ in question 6. (Because you can’t reduce your consumption of meat, when you don’t eat any meat, right?).

How do I do this? I don't even know how to approach this problem :(

Here's a picture to illustrate what I mean: picture of dataframe including mentioned cells

Thanks a lot in advance!!



Solution 1:[1]

You can simply replace it with a nan value.

def delete_cell(x):
     if x == 'never':
          return np.nan
     else:
          return x

df['col9'] = df['col6'].apply(lambda x: delete_cell(x))

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1