在熊猫中使用融化功能

时间:2018-10-15 10:37:41

标签: python pandas

编辑:回答的问题,请参见下面的代码。我忘了加

df = df.melt(id_vars=['Gender', 'Length of service', 'Team'],
        value_vars=Questions,
        var_name='Question',
        value_name='Result')

我一直试图在Pandas中使用melt函数,但效果有限。

我正在尝试获取36个问题,每个ID分为36行。

import pandas as pd

Questions = ['How likely are you to you recommend the company as a place to work to a friend or colleague? ',
 'How satisfied are you in working at the company?','My work gives me a feeling of personal accomplishment',
 'I know what is expected of me and I have clearly defined goals','I have the tools and resources to do my job well',
 'My job makes good use of my skills and abilities',
 'I receive the training I need to do my job well',
 'There are adequate opportunities for career growth in this organisation',
 'I understand the business priorities and how my job can help',
 'I get the right level of support from my Manager',
 'I receive useful and constructive feedback from my Manager',
 'I receive the praise and recognition I deserve from my Manager',
 'My Manager is interested in my professional development and advancement',
 'My Manager treats everyone in the team fairly',
 'My Manager listens to what I\'m saying',
 'I feel comfortable voicing my concerns to my Manager',
 'My Manager keeps me well informed of what is happening',
 'I have confidence in the Executive Team to lead the company',
 'The Executive Team listens to and responds to the needs of employees',
 'I feel I can easily approach and communicate with members of the Executive Team',
 'The Executive Team at the company leads by example',
 'The Executive Team keeps the company well informed of what is happening',
 'This Company supports a good work life balance',
 'The pace of the work in this Company enables me to do a good job',
 'The amount of work I am asked to do is reasonable',
 'My job does not cause unreasonable amounts of stress in my life',
 'My team work well together and support each other when needed',
 'Everybody is treated fairly in this Company',
 'Poor performance is effectively addressed throughout this Company',
 'I can disagree with my manager without fear of getting in trouble',
 'I am comfortable sharing my opinions at work','Diversity is valued at the company',
 'I am proud to work for the company','I am paid fairly for the work I do',
 'My salary is competitive with similar jobs I might find elsewhere',
 'My benefits are comparable to those offered by other Companies']

df = pd.read_excel(r'Survey.xlsx')

df.melt(id_vars=['Gender', 'Length of service', 'Team'],
        value_vars=Questions,
        var_name='Question',
        value_name='Result')

df.to_csv(r'FINAL_OUTPUT.csv', index=False)

但是我上面的代码似乎没有任何作用。 I have attached a .zip file with the Survey.xlsx and associated code。我已经将数据随机化,因此其中没有敏感内容。

有什么想法吗?

谢谢。

1 个答案:

答案 0 :(得分:1)

使用melt

df.melt(id_vars=['Category A', 'Category B', 'Category C'],
        value_vars=['Q1', 'Q2', 'Q3'],
        var_name='Question',
        value_name='Result')

而且,您可以手动将所有内容添加到Q或使用:

df.melt(id_vars=['Category A', 'Category B', 'Category C'],
        value_vars=df.columns[df.columns.str.startswith('Q')],
        var_name='Question',
        value_name='Result')

如果仅所需列以Q开头。