Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

get dataframe name python

import pandas as pd
# Dataframe example
dfA = pd.DataFrame({'col_A':[1,5,7,8],'col_B':[9,7,4,3], 'col_C':[5,1,4,9]})
dfC= pd.DataFrame({'col_A':[3,5,9,8],'col_B':[1,3,3,6], 'col_C':[9,9,1,6]})

# Dataframe list example
df = [dfA,dfC]

# Get the dataframe name (function)

def get_df_name(data):    
    
    if isinstance(data, list): # To get names from list of dataframes
        name = []
        for d in data:
            n = [x for x in globals() if globals()[x] is d][0]
            name.append(n)
        return name    
    else: # To get name from single dataframe
        name =[x for x in globals() if globals()[x] is data][0]
        return name

# Get the names
dfA_name = get_df_name(dfA)
df_List_name = get_df_name(df)

print(f'Single dataframe name: {dfA_name}')
print(f'Names from list of dataframesdf_List_name: {df_List_name[0]} and {df_List_name[1]}')
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #dataframe #python
ADD COMMENT
Topic
Name
7+5 =