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: []
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)
>gapminder.columns = ['country','year','population',
'continent','life_exp','gdp_per_cap']
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]}')