是否可以在函数中传递for循环?

时间:2019-07-01 03:38:41

标签: python pandas scipy anova

我知道这听起来很荒谬,但是我必须将for循环传递给函数。我有一个包含75多个列的数据框,其中大多数是分类变量。变量之一称为SalePrice,我希望找到分类变量与SalePrice之间的相关性。

这是我的代码,但是我认为手动遍历所有75列是荒谬的。有简单的方法吗?

df = pd.read_csv(file, delimiter=',')
qualityTest = df[["OverallQual","SalePrice"]]
qualities = [1,2,3,4,5,6,7,8,9,10]
stats.f_oneway(qualityTest['SalePrice'][qualityTest['OverallQual'] == 1],
              qualityTest['SalePrice'][qualityTest['OverallQual'] == 2],
              qualityTest['SalePrice'][qualityTest['OverallQual'] == 3],
              qualityTest['SalePrice'][qualityTest['OverallQual'] == 4],
              qualityTest['SalePrice'][qualityTest['OverallQual'] == 5],
              qualityTest['SalePrice'][qualityTest['OverallQual'] == 6],
              qualityTest['SalePrice'][qualityTest['OverallQual'] == 7],
              qualityTest['SalePrice'][qualityTest['OverallQual'] == 8],
              qualityTest['SalePrice'][qualityTest['OverallQual'] == 9],
              qualityTest['SalePrice'][qualityTest['OverallQual'] == 10])

我尝试过这样做,但是没有用

stats.f_oneway(
    for i in qualities:
        qualityTest['SalePrice'][qualityTest['OverallQual'] == i]
)

2 个答案:

答案 0 :(得分:5)

您可以使用列表理解-本质上,可以使用 for (int sheet = 1; sheet <= 3; sheet++) { xlworkSheet = (Excel.Worksheet)xlworkbook.Worksheets.get_Item(sheet); //rw is equal to 10 rows for (int row = 0; row <= rw; row++) { //extract out excel value and store it into empID variable var empID=(string)(xlworkSheet.Cells[row,1]as(Excel.Range).Value.ToString(); //store empID into employeeobj employeeobj.employeeID = empID; xlNewSheet2.Cells[row, 1].Value = employeeobj.employeeID; } } 循环创建列表,并将其传递给:

for

或者,如果您希望它作为 stats.f_oneway([qualityTest['salePrice'][qualityTest['OverallQual'] == i] for i in qualities]) 个单独的参数传递,而不是作为一个带有i个元素的列表传递,则可以添加一个{{ 1}}就在最外面的一组方括号的前面(这会将刚刚列出的列表解压缩为函数参数)。

答案 1 :(得分:3)

在此处使用groupby

qualityTest.groupby('OverallQual').OverallQual.apply(stats.f_oneway)