问题描述
Student_ID Subject Class_Feedback_question Class_Feedback_Answer
100101 Physics How was physics class? Excellent: 5
100101 Physics how was physics class: very good:4
100101 Physics How was the Presentation: Good: 3
100101 Chemistry How Much would you rate? Excellent: 5
100102 Math how much would you rate: Excellent: 4
100102 Physics how was presentation: Good: 3
我想让 Class_Feedback_question
列中的相似行值看起来相等,(例如:在问题:
结束时将 ?
设为 or
保持一个问题不变并用它替换其他类似的问题,例如:how was presentation:
被替换为 How was the Presentation:
)
像这样:
Student_ID Subject Class_Feedback_question Class_Feedback_Answer
100101 Physics How was physics class? Excellent: 5
100101 Physics How was physics class? very good:4
100101 Physics How was the presentation: Good: 3
100101 Chemistry How Much would you rate? Excellent: 5
100102 Math How much would you rate? Excellent: 4
100102 Physics How was the presentation: Good: 3
如何在 python Pandas
中完成此操作?
另外,如何基于 Class_Feedback_Answer
创建数据透视表?
解决方法
将“:”替换为“?”您可以使用 apply
方法
df['Class_Feedback_question'] = df['Class_Feedback_question'].apply(x: x.replace(':','?'))
并且要重新表述您必须逐案处理的问题,我建议使用 map
方法
rephrasing = {'how was presentation': 'How was the presentation'}
df['Class_Feedback_question'] = df['Class_Feedback_question'].map(rephrasing)
对于数据透视表,请查看 pandas 文档,因为您没有提供足够的信息。很清楚:https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.pivot.html?highlight=pivot#pandas.DataFrame.pivot