【问题标题】:How to make similar values in a column equal如何使列中的相似值相等
【发布时间】:2021-04-11 22:30:16
【问题描述】:

我有一个这样的数据框:

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 创建数据透视表?

【问题讨论】:

    标签: python-3.x pandas dataframe pivot-table multiple-columns


    【解决方案1】:

    将 ':' 替换为 '?'你可以使用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

    【讨论】:

      猜你喜欢
      • 2012-12-26
      • 2012-03-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多