Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

create dataframe with column names pandas

In [4]: import pandas as pd
In [5]: df = pd.DataFrame(columns=['A','B','C','D','E','F','G'])
In [6]: df
Out[6]:
Empty DataFrame
Columns: [A, B, C, D, E, F, G]
Index: []
Comment

pandas dataframe column names

print(data.columns.values)
Comment

get dataframe column names

# Import pandas package 
import pandas as pd 
    
# making data frame 
data = pd.read_csv("nba.csv") 
  
# iterating the columns
for col in data.columns:
    print(col)
Comment

name columns pandas

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

changing names of column pandas

import pandas as pd

# You don't need these two lines
# as you already have your DataFrame in memory
df = pd.read_csv("nor.txt", sep="|")
df.drop(df.columns[-1], axis=1)

# Get column names
cols = df.columns

# Create a new DataFrame with just the markdown
# strings
df2 = pd.DataFrame([['---',]*len(cols)], columns=cols)

#Create a new concatenated DataFrame
df3 = pd.concat([df2, df])

#Save as markdown
df3.to_csv("nor.md", sep="|", index=False)
Comment

name columns pandas

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

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 :: pip not recognized 
Python :: pandas merge sort columns 
Python :: iterating a list in python 
Python :: python trim zero off end of list 
Python :: python cant remove temporary files 
Python :: python re split 
Python :: accuracy for each class 
Python :: python basics 
Python :: open file in python 
Python :: Generate hashed passwords for ansible 
Python :: ** in python 
Python :: Uninstalling/removing a package is very easy with pip: 
Python :: winsound python 
Python :: string + string in python 
Python :: run python in c ++ 
Python :: Lucky four codechef solution 
Python :: How to Get the Symmetric Difference of Sets in Python 
Python :: read one column pandas 
Python :: python create unreadable save file 
Python :: numpy arange 
Python :: allow x_frame_options django 
Python :: print("hello world") 
Python :: reading files in python 
Python :: add columns not in place 
Python :: hash function in python 
Python :: odoo model 
Python :: python class example 
Python :: use a library in python 
Python :: pandas read csv with lists 
Python :: python dictionary input 
ADD CONTENT
Topic
Content
Source link
Name
1+3 =