2

I got a dataframe which looks like

  Fever   Chill   Cough   Headache  Respiratory   Nasal   Joint Pain    Back pain   Stomach pain        
0   Y       Y        Y     NaN           NaN         NaN    NaN          NaN        NaN 
1   Y       NaN      NaN    NaN          Y           NaN    NaN          NaN        NaN 
2   Y       NaN       Y     NaN          NaN         NaN    NaN          NaN        NaN 
3   Y       NaN      NaN    NaN          NaN         NaN    Y            NaN        NaN 
4   Y       NaN      NaN    NaN          NaN         NaN    NaN          NaN        NaN 

I want to convert columns header into single column (let say symptom) in such a way that newly column contains the column header name when it has value Y. Desired column should be look like:

      Symptom
0     Fever, Chill, Cough
1     Fever, Respiratory
2     Fever, Cough
3     Fever, Joint Pain
4     Fever

I used the concept of stack, but it didnt produce the desired ouput. Here is my code:

df[['Fever','Chill','Cough','Headache','Respiratory symptom','Nasal Symptoms','Joint Pain','Back pain','Stomach pain','Diarrhoea','Vomiting','Fatigue','Pneumonia shadow']].stack().reset_index()

Could anyone guide me how to get the desired result?

1 Answer 1

4

Let us do dot

df.eq('Y').dot(df.columns+',').str[:-1]
#df['symptom'] = df.eq('Y').dot(df.columns+',').str[:-1]
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.