在 Python 中定义函数时如何调用标题作为参数?
How do I call a heading as an argument when defining a function in Python?
我有一个看起来像这样的数据框:
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
您可以通过属性或项目访问熊猫相框。编码时必须知道属性,项目必须是字符串,因此可以是变量。
df.Major
对比
df['Major']
我建议,将切断和转换为列表分开:
def cutoff(df, category, cut, direction):
mask = df[category] < cut if direction == 0 else df[category] > cut
return df[mask]
def get_list(df, category):
return list(zip(df.Major, df[category]))
get_list(cutoff(df, 'Employed', 1000, 1))
我有一个看起来像这样的数据框:
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
您可以通过属性或项目访问熊猫相框。编码时必须知道属性,项目必须是字符串,因此可以是变量。
df.Major
对比
df['Major']
我建议,将切断和转换为列表分开:
def cutoff(df, category, cut, direction):
mask = df[category] < cut if direction == 0 else df[category] > cut
return df[mask]
def get_list(df, category):
return list(zip(df.Major, df[category]))
get_list(cutoff(df, 'Employed', 1000, 1))