簡體   English   中英

循環遍歷 python 中的函數列表:

[英]Looping through a list of functions in python:

以下代碼非常適用於 function df.max(axis=1),如下所示。 請參閱將 function 方法傳遞給 python 中的 class:了解背景信息。

import pandas as pd
df = pd.DataFrame( {'col1': [1, 2], 'col2': [4, 6]})

class our_class():
    def __init__(self, df):
        self.df = df
    
    def arb_func(self, func):
        return func(self.df)

ob1 = our_class(df)
print(ob1.arb_func(lambda x: x.max(axis=1)))

我想通過循環遍歷函數列表來概括這一點。 我在下面的嘗試失敗了:

func_list = [max(axis=1), min(axis=0), mean()]

for f in func_list:
      print(ob1.arb_func(lambda x: x.f))

任何意見或建議表示贊賞。

這里軸 1 表示逐行

import pandas as pd
df = pd.DataFrame( {'col1': [1, 2], 'col2': [4, 6]})

class our_class():
    def __init__(self, df):
        self.df = df
    
    def arb_func(self, func):
        return self.df.apply(func,axis=1)

ob1 = our_class(df)
print(ob1.arb_func(lambda x: x.max()))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM