简体   繁体   English

如何将数据框单元格值作为传递给函数的变量名返回

[英]How to return a data frame cell value as the variable name passed to a function

I made a function that returns a few statistics, but I would like the last column to be the name of the parameter that is inserted into the function.我创建了一个返回一些统计信息的函数,但我希望最后一列是插入到函数中的参数的名称。

The code below shows the function and I would like the last column "Region" to give the value of the parameter that I used to initiate the function "NameofRegion"下面的代码显示了该函数,我希望最后一列“Region”给出我用来启动函数“NameofRegion”的参数值

def roiStats(roi):
    data = {'Mean': np.mean(roi),
            'Median': np.median(roi),
            'StdDev': np.std(roi),
            'Min': np.min(roi),
            'Max': np.max(roi),
            'Region': NameofRegion}
    df = pd.DataFrame(data, index=[0])
    return df

roiStats(NameofRegion)

I would think the answer is:我想答案是:

... "Region": roi ... "Region": roi

But that returns an error: "Must be 1 dimensional".但这会返回一个错误:“必须是一维的”。 So the biggest issue is that 'NameofRegion' is a cropped area of an image;所以最大的问题是“NameofRegion”是图像的裁剪区域; A 2d array of pixel values.像素值的二维数组。

Suppose you have regions a, b, c, d.假设您有区域 a、b、c、d。 Instead of doing something like:而不是做类似的事情:

roia = #something a
roib = #something b
roic = #something c
roid = #something d

Consider doing:考虑做:

roi_dict = {
'a': #something a, 
'b': #something b,
'c': #something c,
'd': #something d
}

Then you can do like this:然后你可以这样做:

def roiStats("NameOfRegion", roi_dict):
    roi = roi_dict["NameOfRegion"]
    data = {'Mean': np.mean(roi),
            'Median': np.median(roi),
            'StdDev': np.std(roi),
            'Min': np.min(roi),
            'Max': np.max(roi),
            'Region': "NameofRegion"}
    df = pd.DataFrame(data, index=[0])
    return df

roiStats("NameofRegion", roi_dict)

Note that now NameofRegion will be a string and thus you can print it请注意,现在 NameofRegion 将是一个字符串,因此您可以打印它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM