0

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)
0

2 Answers 2

2

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?

Sign up to request clarification or add additional context in comments.

Comments

1

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))

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.