How can I write a function that takes in a variable and use the name of the variable to assign it as a string?
def one_function(dataframe):
<does some numeric operation...>
print('dataframe')
so for example
df_wow=pd.DataFrame([1,2,3])
one_function(df_wow)
'df_wow'
Different_data_frame=pd.DataFrame([99,98,100])
one_function(Different_data_frame)
'Different_data_frame'
EDIT
After doing some more research and with the comments and replies I came to the conclusion that it can't be done. I got around this by just passing another argument
def one_function(dataframe,name_arg):
<does some numeric operation...>
print(name_arg)
My end goal here was actually to name the index of the data frame. So actually I ended up writing the following function
def one_function(dataframe,name_arg):
<does some numeric operation...>
dataframe.index.name=name_arg
return(dataframe)
-
2The question is, what for?Błotosmętek– Błotosmętek2017年07月28日 20:13:51 +00:00Commented Jul 28, 2017 at 20:13
-
2Quite simply, you can'tOfer Sadan– Ofer Sadan2017年07月28日 20:18:44 +00:00Commented Jul 28, 2017 at 20:18
1 Answer 1
No you cant, when passing a parameter only the value of the parameter is passed (copied) to the function.
However, you can achieve something similar if you use keyword arguments:
In [1]: def one_function(**kwargs):
...: print(kwargs.keys()[0])
...:
In [2]: one_function(foo=5)
foo
This example prints one (random) argument, if you have more than one you can loop through the .keys() list and print them all or do whatever you want with them.
Comments
Explore related questions
See similar questions with these tags.