从列表中指定函数中的参数

时间:2019-04-26 00:53:17

标签: python pandas argument-unpacking

我已经阅读了许多线程(herehere)和文档(herehere)。但是,我无法使它正常工作。我收到

的错误
AxisError: axis 0 is out of bounds for array of dimension 0

谢谢。

import pandas as pd
from scipy.stats import levene

data = {'A': [1,2,3,4,5,6,7,8],
        'B': [9,10,11,12,13,14,15,16],
        'C': [1,2,3,4,5,6,7,8]}
df3 = pd.DataFrame(data, columns=['A', 'B','C'])

print(levene(df3['A'], df3['C'])) # this works as intended

cols_of_interest = ['A','C'] # my requirement could make this any combination

# function to pass through arguments into Levene test
def func(df,cols_of_interest):
    cols = [col for col in 'df.'+df[cols_of_interest].columns] # my strategy to mimic the arguments
    lev = levene(*cols)
    print(lev)

func(df3,cols_of_interest)

1 个答案:

答案 0 :(得分:1)

def中的列表理解替换为:

cols = [df[x] for x in cols_of_interest]