简体   繁体   English

基本的python定义函数

[英]Basic python defining function

I'm having difficulty applying my knowledge of defining functions with def to my own function.我很难将我用 def 定义函数的知识应用到我自己的函数中。

I want to create a function where I can filter my data frame based on my 1. columns I'd like to drop + their axis 2. using .dropna我想创建一个函数,我可以在其中根据我的 1. 我要删除的列 + 他们的轴 2. 使用 .dropna 过滤我的数据框

I've used it on one of my data frames like this :我已经在我的一个数据框上使用了它,如下所示:

total_adj_gross = ((gross.drop(columns = ['genre','rating', 'total_gross'], axis = 1)).dropna())

I've also used it on another data frame like this :我也在这样的另一个数据框上使用过它:

vill = (characters.drop(columns = ['hero','song'], axis = 1)).dropna(axis = 0)

Can I make a function using def so I can easily do this to any data frame?我可以使用 def 创建一个函数,以便我可以轻松地对任何数据框执行此操作吗?

if so would I go about it like this如果是这样,我会这样做吗

def filtered_df(data, col_name, N=1): 
 frame = data.drop(columns = [col_name], axis = N)
 frame.dropna(axis = N) 

 return frame 

I can already feel like this above function would go wrong because what if I have different N's like in my vill object?我已经可以感觉到上面的函数会出错,因为如果我的 vill 对象中有不同的 N 怎么办?

BTW I am a very new beginner as you can tell -- I haven't been exposed to any complex functions any help would be appreciated!顺便说一句,正如您所知,我是一个非常新的初学者——我没有接触过任何复杂的功能,任何帮助将不胜感激!


Update since I dont know how to make a code in comments:更新,因为我不知道如何在评论中制作代码:

Thank you all for your help in creating my function but now how do I insert this in my code?感谢大家在创建我的函数时提供的帮助,但现在我如何将它插入到我的代码中?

Do I have to make a script (.py) then call my function?我必须制作一个脚本(.py)然后调用我的函数吗?

can I test in within my actual code?我可以在我的实际代码中进行测试吗?

right now if I just copy + paste any code in, and fill the column name I get an error saying the specific column code "is not found in the axis"现在,如果我只是复制 + 粘贴任何代码并填写列名,我会收到一条错误消息,指出特定列代码“在轴中找不到”

Based on what you want to achieve, you don't need to pass any axis parameter.根据您想要实现的目标,您不需要传递任何轴参数。 Also, you want to pass a list of columns as a parameter to drop the different columns (axis=1 for drop() and axis=0 for dropna() , which is the default parameter value).此外,您希望将列列表作为参数传递以删除不同的列(对于drop() ,axis=1,对于dropna() ,axis=0,这是默认参数值)。 And finally, dropna() is not in place by default.最后,默认情况下dropna()不存在。 You have to store the returned value into a frame like you did the line above.您必须像上面的行一样将返回的值存储到一个框架中。

Your function should look like that:您的函数应如下所示:

def filtered_df(data, col_names): 
 frame = data.drop(columns = col_names, axis = 1)
 result = frame.dropna() 
 return result 

Overall, code looks good.总的来说,代码看起来不错。 I'd suggest 3 minor changes:-我建议 3 个小改动:-

  1. Pass columns names as list.将列名称作为列表传递。 Do not convert them to list within the functions不要将它们转换为函数中的列表
  2. Pass 2 variables for working with axis.传递 2 个变量以使用轴。 From what i see in your eg, your axis values changes for drop and dropna.从我在你的例子中看到的,你的轴值改变了 drop 和 dropna。 Not sure about your need for it.不确定您是否需要它。 If you want 2 diff axis values for drop() and dropna() then please use 2 diff variables, probably like drop_axis and dropna_axis .如果你想要drop()dropna()的 2 个 diff 轴值,那么请使用 2 个 diff 变量,可能像drop_axisdropna_axis
  3. assigning modified frame / single line operation分配修改的帧/单行操作

So, code would look something like this:-所以,代码看起来像这样:-

def filtered_df(data, col_name, drop_axis=1, dropna_axis=0): 
    frame = data.drop(columns = col_name, axis = drop_axis).dropna(axis = dropna_axis)
    return frame 

Your call to it can look like:您对它的调用可能如下所示:

modified_df = filtered_df(data, ["x_col","y_col"], 0, 0)

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

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