Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python:将列标题转换为单列值_Python_Python 3.x_Pandas - Fatal编程技术网

Python:将列标题转换为单列值

Python:将列标题转换为单列值,python,python-3.x,pandas,Python,Python 3.x,Pandas,我有一个数据框,看起来像 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

我有一个数据框,看起来像

  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 
我想将columns header转换为单列(比如说症状),以便新列在其值为Y时包含列标题名称。所需列应如下所示:

      Symptom
0     Fever, Chill, Cough
1     Fever, Respiratory
2     Fever, Cough
3     Fever, Joint Pain
4     Fever
我使用了堆栈的概念,但它没有产生所需的输出。这是我的密码:

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

有谁能指导我如何得到想要的结果吗?

让我们做
dot

df.eq('Y').dot(df.columns+',').str[:-1]
#df['symptom'] = df.eq('Y').dot(df.columns+',').str[:-1]