【问题标题】:Is there a way to incorporate f strings in pandas loc output有没有办法将 f 字符串合并到 pandas loc 输出中
【发布时间】:2021-07-25 15:32:17
【问题描述】:

我正在尝试设置一个脚本,该脚本将为学生报告生成 cmets。为了设置一个通用模板,我使用了一个包含百分比分数的分数表。我无法使用 f 字符串插入每个学生的姓名,以及任何相关的代词或教学科目。

目前,我已经写了这个:

report = pd.DataFrame({'Preferred Name': ['Jack', 'Jenny', 'Bob'],
                       'Topic': ['English', 'English', 'Maths'],
                       'HomeworkAv': [84.6, 68, 94.1]})
name = report['Preferred Name']
topic = report['Topic']
report.loc[report['HomeworkAv'] >= 80, 'TopicSentence'] = f"{name} has consistently demonstrated an excellent understanding for {topic} throughout the entire term. "

问题在于,每次在 f 字符串中插入一个变量,它都会列出该变量中的每个值。我可以看到明确的问题是什么,但我不知道如何解决它。

有没有办法可以做到这一点?

【问题讨论】:

    标签: python pandas dataframe f-string pandas-loc


    【解决方案1】:

    您可以使用apply 语句完成它。

    我将这样做:

    import pandas as pd
    report = pd.DataFrame({'Preferred Name': ['Jack', 'Jenny', 'Bob'],
                           'Topic': ['English', 'English', 'Maths'],
                           'HomeworkAv': [84.6, 68, 94.1]})
    
    report['TopicSentence'] = report.apply(lambda x: f"{x['Preferred Name']} has consistently demonstrated an excellent understanding for {x['Topic']} throughout the entire term." if x['HomeworkAv'] >= 80 else None, axis=1)
                                    
    print (report)
    

    或者你也可以这样做:

    notes = "{} has consistently demonstrated an excellent understanding for {} throughout the entire term."
    report['TopicSentence'] = report.apply(lambda x: notes.format(x['Preferred Name'],x['Topic']) if x['HomeworkAv'] >= 80 else None, axis=1)
    

    输出将是:

      Preferred Name  ...                                                                              TopicSentence
    0           Jack  ...  Jack has consistently demonstrated an excellent understanding for English throughout t...
    1          Jenny  ...                                                                                       None
    2            Bob  ...  Bob has consistently demonstrated an excellent understanding for Maths throughout the ...
    

    【讨论】:

      【解决方案2】:

      如果你想用.loc()方法和f-string一起实现任务,你可以这样做:

      report.['TopicSentence'] = ''     # initialize the sentence to ''
      report.loc[report['HomeworkAv'] >= 80, 'TopicSentence'] = report.loc[report['HomeworkAv'] >= 80].apply(lambda x: f"{x['Preferred Name']} has consistently demonstrated an excellent understanding for {x['Topic']} throughout the entire term. ", axis=1)
      
      
      print(report)
      
        Preferred Name    Topic  HomeworkAv                                                                                           TopicSentence
      0           Jack  English        84.6  Jack has consistently demonstrated an excellent understanding for English throughout the entire term. 
      1          Jenny  English        68.0                                                                                                        
      2            Bob    Maths        94.1     Bob has consistently demonstrated an excellent understanding for Maths throughout the entire term. 
      

      【讨论】:

        【解决方案3】:

        使用numpy它是fast

        import numpy as np
        report['HomeworkAv'] = np.where(report['HomeworkAv'] >= 80 , report['Preferred Name'] +  'has consistently demonstrated an excellent understanding for' + report['Topic'] + 'throughout the entire term', '')
        

        注意:如果您有多个条件,请使用np.select

        【讨论】:

          猜你喜欢
          • 2022-01-07
          • 2020-09-10
          • 2021-02-28
          • 2023-04-07
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-09-27
          相关资源
          最近更新 更多