我有一个看起来像这样的数据框:
Major Sample_size Men Women ShareWomen Employed ... Part_time
Economics 36 2057 282 0.120564 1976 ... 270
French 7 679 77 0.101852 640 ... 170
我正在尝试定义一个函数,如下所示:
def cutoff(category, cut, direction):
if direction == 0:
comply = list(zip(df[df.category < cut].Major, df[df.category < cut].category))
if direction == 1:
comply = list(zip(df[df.category > cut].Major, df[df.category > cut].category))
return comply
其中category
是指感兴趣的变量(例如Men
或Employed
或Part_time
) 。但我似乎无法以这种方式调用 category
作为输入变量。人们会怎样做呢?
最佳答案
您可以使用df[category]
def cutoff(category, cut, direction):
if direction == 0:
comply = list(zip(df[df[category] < cut].Major, df[df[category] < cut][category]))
if direction == 1:
comply = list(zip(df[df[category] > cut].Major, df[df[category] > cut][category]))
return comply
关于python - 在 Python 中定义函数时,如何将标题作为参数调用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28779122/