【问题标题】:Applying a function for multiple dataframes为多个数据框应用一个函数
【发布时间】:2022-11-20 00:43:36
【问题描述】:

我正在尝试应用该功能来申请多个数据帧。我为数据框创建了一个列表。如果排名小于 100,高性能列将从排名列复制的值分配,如果排名在 100 和 200 之间,平均列将分配从排名列复制的值。如果排名介于 200 和 300 之间,则性能较低的列将从排名列中复制分配的值。运行脚本时没有收到任何错误消息,但该函数未应用于数据框。任何的意见都将会有帮助。


for file in tests: #tests would be a list of data frame
    def func (file):
    
        if (file['ranking']) < 100:
            (file['ranking']) == (file['High Performance'])
        elif (file['ranking']) > 100 & (file['ranking'] < 200):
            (file['ranking'])== (file['Average'])
        elif (file ['ranking']) > 200& (file['ranking'] < 300):
            (file['ranking']) == (file ['Low Performance'])
        else: 
            return ''

file['High Performance'] = file.apply(func, axis=1)
file['Average'] = file.apply(functionss, axis=1)
file['Low Performance'] = file.apply(functionss, axis=1)

【问题讨论】:

    标签: python list


    【解决方案1】:

    你不会出错,因为你的代码在语法上是正确的。但是要注意逻辑。我希望以下代码更改有所帮助:

    def func (file):
        if (file['ranking']) < 100:
            (file['ranking']) == (file['High Performance'])
        elif (file['ranking']) > 100 & (file['ranking'] < 200):
            (file['ranking'])== (file['Average'])
        elif (file ['ranking']) > 200& (file['ranking'] < 300):
            (file['ranking']) == (file ['Low Performance'])
        else: 
            return ''
                
    for file in tests: #tests would be a list of data frame
        file['High Performance'] = file.apply(func, axis=1)
        file['Average'] = file.apply(functionss, axis=1)
        file['Low Performance'] = file.apply(functionss, axis=1)
    

    【讨论】:

      猜你喜欢
      • 2011-06-15
      • 1970-01-01
      • 1970-01-01
      • 2011-11-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-03
      相关资源
      最近更新 更多