Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

name columns pandas

df.columns = ['column1', 'column2', 'column3']
df.columns
Comment

name columns pandas

>gapminder.columns = ['country','year','population',
                     'continent','life_exp','gdp_per_cap']
Comment

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 :: sklearn.metrics accuracy_score 
Python :: python string: .replace() 
Python :: math floor python 
Python :: python hex 
Python :: is_integer python 
Python :: list all pip packages 
Python :: min max python 
Python :: how to read mysql table in python 
Python :: setdefault python 
Python :: python how to find the highest even in a list 
Python :: last element python 
Python :: how to use multiple keys for single value in dictionary python 
Python :: generator comprehension python 
Python :: how to find a key in a dictionary python 
Python :: python __new__ 
Python :: reading an image using opencv library 
Python :: on_delete django options 
Python :: check if object is list python 
Python :: math function in python 
Python :: python pattern 
Python :: calculate sum in 2d list python 
Python :: python3 password generator script 
Python :: python port forwarding 
Python :: how to clear combobox tkinter 
Python :: python how do index all odd numbers in a list 
Python :: django.db.utils.IntegrityError: NOT NULL constraint failed 
Python :: bitcoin with python 
Python :: python for loop inside list 
Python :: forward checking algorithm python 
Python :: python web server oneliner 
ADD CONTENT
Topic
Content
Source link
Name
9+7 =