我有一个看起来像这样的数据框:
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
