How to get the name of a variable as a string? [duplicate] - python

This question already has answers here:
How to get the original variable name of variable passed to a function [duplicate]
(13 answers)
Closed 2 years ago.
I'm writing a function to draw plots of dataframes and I would like to name the plots according to the name of the dataframe passed to the function. The first line of the plotter function is:
def plotter(data):
Say the data frame to be passed is df1, I'm wondering how could I get the string 'df1' when calling plotter(data=df1)?

You can do this:
from varname import nameof
def plotter(data, name_df):
# name_df is a string which is is "df1", then you can add to the plot
plotter(df1, nameof(df1))

Related

Getting the value of a variable when I only have the variable name as a string [duplicate]

This question already has answers here:
How do I create variable variables?
(17 answers)
Closed 1 year ago.
x = 14
variable_name = 'x'
How can I get the value of x when I only have the name of it as a string?
You can use vars():
vars()[variable_name]
Which gives the variables of the scope it's called at (Source).

I want print paramater's name in a python function [duplicate]

This question already has answers here:
How to print actual name of variable class type in function?
(4 answers)
Getting the name of a variable as a string
(32 answers)
Closed 3 years ago.
I want print paramater's name in a python function
First, a variable is assigned a value.Then, pass the variable to a function.
Last,I want to print str(variable) inner the function.
varibale = 1234
def f(x):
print(....)
return
f(varibale)
Expected output is print out the 'varibale' whatever variable is.
if a = 1 ==> f(x), expected output is 'a';if b = 2 ==> f(x), expected output is 'b'.....
Why do you want to print out the value using a function?
If you know the name of the variable, you can simply write your requested function output yourself by just putting 'variable'
So in your example: instead of using f(x), just use print('x')

Variable of Variable in Python [duplicate]

This question already has answers here:
How can I select a variable by (string) name?
(5 answers)
Closed 3 years ago.
I've a list of variable names, how can I print value of it using for loop..
var1="First"
var2="Second"
list=["var1","var2"]
for var in list:
print(var) # I want to print value of var,
present output
var1
var2
What I want is
First
Second
list=["var1","var2"]
You've made a list of strings, not variable names. What you need is
list=[var1,var2]

Print variable name in python [duplicate]

This question already has answers here:
Simpler way to create dictionary of separate variables?
(27 answers)
Closed 4 years ago.
Here bkd_train, bkd_test are dataframe.
I wanted to print dataframe name along with it's shape.
for data in [bkd_train, bkd_test]:
print("{} : {}".format(?, data.shape))
If i am using "data" then it's printing dataframe.
But I want o/p like this:
bkd_train : (10886, 12)
bkd_test : (1111,11)
you cannot get the name of a variable as string, but you can pass a string in input and query locals dictionary to get the value:
for data in ["bkd_train", "bkd_test"]:
print("{} : {}".format(data,locals()[data].shape))
it works if the variables are local. If they're global, use globals, with fallback on locals, or just eval (not a problem in that context since the strings to evaluate are constant)
also read: How to get a variable name as a string?
I would iterate over the variable names themselves and then use eval to evaluate the names which will give you the dataframe.
for dataName in ['bkd_train', 'bkd_test']:
data = eval(dataName)
print("{} : {}".format(dataName, data.shape))

Python: Getting variable name out of List within For loop [duplicate]

This question already has answers here:
How do I create variable variables?
(17 answers)
Convert string to variable name in python [duplicate]
(3 answers)
Closed 5 years ago.
How to get a variable name out of a List within a for loop.
example:
List = ['a','b','c','d']
for i in List:
var_i_ = something
so for each letter in list there should be a new variable name like:
var_a_ = something
var_b_ = something
var_c_ = something
var_d_ = something
So i could call use it in pandas as well
List = ['a','b','c','d']
for i in List:
df_i_ = something
df_a_ = something
df_b_ = something
df_c_ = something
df_d_ = something
each needs to be a different dataframe.
I need it as a search function to save data as csv or something else.

Categories