physiker
physiker

Reputation: 919

How to print dataframe name in title of a plot?

I have a function which gets a data frame and one column and after some process, plot that column, as in the following line:

def plot_dist(df,col):
    ax=sns.countplot(x=col,data=df)

As i repeat this function for several dataframes, I'd like to have the dataframe name in the title of the plot, like this: "Distribution of col in dataframe df"

plt.title('Distribution of '+ col + 'in dataframe' + df.name );

Q: how to get dataframe name? According to here, one can write df.name='DFNAME' and then get the string by df.name. But then one has to define the name and I am not sure if it works in the loop. Thank you!

Upvotes: 5

Views: 11002

Answers (3)

Quant1892387
Quant1892387

Reputation: 63

The question of: What if this is imported from a module, the function that names the df is in the imported module and, the scope is in a file in a parent directory remains open.

@erp_da

This is a ridiculously over-engineered version that adds thousands separator.

def dfshape(df):
    dfname =[x for x in globals() if globals()[x] is df][0]
    print('Dataframe ['+str(dfname)+"]'s"+f" shape is: ({int(str(df.shape)[1:-1].split(', ')[0]):,})",
          f"({int(str(df.shape)[1:-1].split(', ')[1]):,})")

output:

Dataframe [NAME_OF_DATAFRAME]'s shape is: (2,942,528) (7)

Credits to cors for original solution.

Upvotes: 1

erp_da
erp_da

Reputation: 319

As a beginner I was amazed how difficult it was for experienced programmers to get what we try to achieve. In my case I just wanted to print the name and size of the dataframe. I hope this helps:

def dfshape(df):
    dfname =[x for x in globals() if globals()[x] is df][0]
    print("'"+str(dfname)+"'"+" dataframe shape is:"+str(df.shape))

This if very over-engineered compared to hard-coding the print statement, but does 2 in 1. Credits to cors for original solution.

Upvotes: 2

cors
cors

Reputation: 537

I found nice function here: (Get the name of a pandas DataFrame)

def get_df_name(df):
    name =[x for x in globals() if globals()[x] is df][0]
    return name

It will help you.

def plot_dist(df,col):
    ax=sns.countplot(x=col,data=df)
    ax.set_title(get_df_name(df))

Upvotes: 11

Related Questions