简体   繁体   English

如何在 Python Z251D2BBFE9A3DC674AZ?

[英]How to implement list of variables to function from DataFrame in Python Pandas?

How can I create funcion in python where in one of parameters I will have possibility to implement list of varaibles of DataFrame?如何在 python 中创建函数,在其中一个参数中我将有可能实现 DataFrame 的变量列表?

For instance I have DataFrame like below:例如我有 DataFrame 如下所示:

df = pd.DataFrame({"TARGET" : [1,0,1],
                   "age" : [22, 54, 67],
                   "power" : [100, 200, 345],
                   "height" : [180, 190, 178]})

And for instance I have function:例如我有 function:

def my_function(data, variables, target_variable):
    .....

And in:在:

  • data - I would like to implement my DataFrame (df)数据 - 我想实现我的 DataFrame (df)

  • target_variable - I would like to impelment target variable of my DataFrame ("TARGET") target_variable - 我想实现我的 DataFrame ("TARGET") 的目标变量

  • variables - list of selected variables from DataFrame for instance ("age" and "power")变量 - 例如从 DataFrame 中选择的变量列表(“年龄”和“功率”)

    my_function(df, ["age", "power"], "TARGET") - does not work my_function(df, ["age", "power"], "TARGET") - 不起作用

something to the effect:大意是:

df.apply(my_function)

where在哪里

def my_function(x):
    x['age'] 
    x['power']  
    x['height']  
    x['TARGET']
    return x 

once you pass the row in, you do operations on each row by calling your data as noted above.将行传入后,您可以通过调用上述数据对每一行进行操作。 See the apply() and groupby() for passing data to functions请参阅 apply() 和 groupby() 将数据传递给函数

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

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