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)
Aran-Fey
  • 39,665
  • 11
  • 104
  • 149
Noob
  • 174
  • 1
  • 2
  • 19

2 Answers2

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?

Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
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))
R Parini
  • 21
  • 2