Search
 
SCRIPT & CODE EXAMPLE
 

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]}')
Comment

PREVIOUS NEXT
Code Example
Python :: formatting strings in python 
Python :: how to print multiple strings on one line in python 
Python :: false python 
Python :: how to store a return value in a variable in python 
Python :: list inside a list in python 
Python :: how to print even numbers in python 
Python :: if it is square python 
Python :: address already in use 
Python :: characters python 
Python :: python dictionary accessing an element 
Python :: how to work with django ornm __in 
Python :: change value of column in pandas 
Python :: joining two lists in python using for loop 
Python :: python print array line by line 
Python :: python if loop 
Python :: iterator in python 
Python :: python if elif else syntax 
Python :: python true and false 
Python :: axes_style seaborn 
Python :: selenium python get image from url 
Python :: read header of csv file python 
Python :: numpy find index of matching values 
Python :: install multiple versions of python 
Python :: how to create Varible in python 
Python :: three different randomn numbers python 
Python :: time complexity of data structures in python 
Python :: "scrapy shell" pass cookies to fetch 
Python :: get the largest of 2 strings python 
Python :: show only lower diagonal in sns pairplot 
Python :: john cabot 
ADD CONTENT
Topic
Content
Source link
Name
7+3 =