0

I have these datasets and this loop:

df1 = np.array([[91,9], [22,78]])
df2 = np.array([[99,1], [21,79]])
df3 = np.array([[96,4], [10,90]])
df4 = np.array([[98,2], [18,82]])

dfs = [df1, df2, df3, df4]
for i in dfs:
    tpr = i[1,1] / (i[1,0] + i[1,1])
    print('tpr: ', tpr)

and the output is:

tpr:  0.78
tpr:  0.79
tpr:  0.9
tpr:  0.82

How would I get an output like this?

df1 -- tpr:  0.78
df2 -- tpr:  0.79
df3 -- tpr:  0.9
df4 -- tpr:  0.82

The main issue is that I need the name of the array to be printed. I looked at a few other answers (different problems) and tried these, but can't get it to work. For example [i] and other things in the print statement to try and get the name.

e.g. How to print the list in 'for' loop using .format() in Python?, but the problem is different and doesn't seem to apply.

martineau
  • 119,623
  • 25
  • 170
  • 301
william3031
  • 1,653
  • 1
  • 18
  • 39
  • Perhaps this can be of help? https://stackoverflow.com/questions/18425225/getting-the-name-of-a-variable-as-a-string – RasmusN May 22 '20 at 03:02
  • Why not just use a data structure that explicitly stores names and associates them with the arrays - such as a dict, or a list of tuples? – Karl Knechtel May 22 '20 at 03:25

3 Answers3

2

The best thing is probably to use a dictionary to store your arrays.

dfs = {'df1': df1, 'df2': df2, 'df3': df3, 'df4': df4}

Then you could iterate through the keys to get their names, and then index the dictionary for their values.

for i in dfs.keys():
    val = dfs[i]
    tpr = val[1,1] / (val[1,0] + val[1,1])
    print(i, '-- tpr: ', tpr)

Alternatively you could install varname and get the names with varname.nameof().

duckboycool
  • 2,425
  • 2
  • 8
  • 23
2

You can retrieve the variable name from globals

import numpy as np

df1 = np.array([[91,9], [22,78]])
df2 = np.array([[99,1], [21,79]])
df3 = np.array([[96,4], [10,90]])
df4 = np.array([[98,2], [18,82]])

dfs = [df1, df2, df3, df4]
for i, df in enumerate(dfs):
    tpr = df[1,1] / (df[1,0] + df[1,1])
    # Use generator to find name of current df in global names table
    name = next(x for x in globals() if globals()[x] is df)
    print(f'{name} -- tpr: {tpr}')

Output

df1 -- tpr: 0.78
df2 -- tpr: 0.79
df3 -- tpr: 0.9
df4 -- tpr: 0.82
DarrylG
  • 16,732
  • 2
  • 17
  • 23
1

Please note that the name you use for a variable is not something that you should involve in your codes logic - that's just a plain bad idea.

However - to know from which dataframe your output is coming from - you can print the index of that datafram in the loop:

import numpy as np

df1 = np.array([[91,9], [22,78]])
df2 = np.array([[99,1], [21,79]])
df3 = np.array([[96,4], [10,90]])
df4 = np.array([[98,2], [18,82]])

dfs = [df1, df2, df3, df4]
for i, df in enumerate(dfs):
    tpr = df[1,1] / (df[1,0] + df[1,1])
    print(f'df{i+1} -- tpr: {tpr}')

Output:

df1 -- tpr: 0.78
df2 -- tpr: 0.79
df3 -- tpr: 0.9
df4 -- tpr: 0.82

I used i+1 to get the same output you wanted but using just i would be more accurate.

rdas
  • 20,604
  • 6
  • 33
  • 46
  • Why not name things and print them in useful ways? "df1" is a bit mundane but if OP wants to name things and print things similarly, seems reasonable to me. – tdelaney May 22 '20 at 03:14