Skip to content
Advertisement

how can i def function for new Dataframe with Cleaned data

I have several dataframes where I need to reduce the dataframe to a time span for all of them. So that I don’t have to reduce the codeblock over and over again, I would like to write a function.

Currently everything is realized without working by the following code:

JavaScript

my approach:

JavaScript

unfortunately this does not work

Advertisement

Answer

There are a few mistakes in your approach. Firstly, when you create a new variable you need to specify exactly what it will be called. It is not possible to “dynamically” name a variable like you’re trying with 'df_' + name = something.

Second, variable scope dictates that any variable created in a function is only accessible inside that function, and ceases to exist once it finishes executing (unless you play special tricks with global variables). So, even if you did df_name = Data_1.loc[Data_1[column] >= t], once Time_range() finishes running, that variable will be deleted.

What you can do is have the function return the finished DataFrame and assign the result as a new variable from the outside:

JavaScript

Generally, this is what you want functions to do. Do some operations and return a finished value that you can then use from the outside.

Advertisement