Python Forum
help with exporting functions to excel - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Data Science (https://python-forum.io/forum-44.html)
+--- Thread: help with exporting functions to excel (/thread-23185.html)



help with exporting functions to excel - avanage - Dec-15-2019

Hi,
I am trying to analyze economic freedom index from https://www.heritage.org/index/about
I have written following code and want to export everything to excel where i am supposed to use tableau for creating charts (currently we are not using TabPy as we have been asked not to use it.

We are to export all the functions like data.head(), mean, median, mode, corr, et al in to excel and submit the project. Unfortunately, we are unable to do this as it says that DataFrame objects are mutable and hence, they cannot be hashed. Anyone has an idea on how we can clear this dataframe TypeError?


import pandas as pd
import os
os.chdir(r"D:\Documents\NJCU\Intro to Data Science\Final Project\the-economic-freedom-index")

data = pd.read_csv("economicfreedom.csv",encoding='latin-1')
data.columns = [c.replace(' ', '_') for c in data.columns]
data.columns = [c.replace("'", '') for c in data.columns]

print(data.head())
data.info().to_excel('Economic Index Analysis.xlsx', sheet_name='Data Info')

info_dict = {data.head()}
datinfo_write = pd.DataFrame({info_dict})
datinfo_writer = pd.ExcelWriter('Economic Index Analysis.xlsx',engine='xlswriter')
datinfo_write.to_excel(datinfo_writer,sheet_name='Data Info')
Error:
TypeError: 'DataFrame' objects are mutable, thus they cannot be hashed



RE: help with exporting functions to excel - sandeep_ganga - Dec-24-2019

If you are trying to export to new csv/excel file having head(), describe(), mean(),median(), mode() etc of given input file, you may try below and see if that helps.

import pandas as pd
 
data = pd.read_csv("inputbook.csv",encoding='latin-1')
data.columns = [c.replace(' ', '_') for c in data.columns] 
data.columns = [c.replace("'", '') for c in data.columns]

info_dict = data.head()
descdata=data.describe()
meandata=data.mean()
mediandata=data.median()
modedata=data.mode()

db=pd.DataFrame(columns=['------------empty line-----------------'])


open("output.csv", 'w')  #empty the csv file
descdata.to_csv("output.csv", mode='a', header=True)
db.to_csv('output.csv',mode='a',header=True)  #add empty line
info_dict.to_csv('output.csv', mode='a', header=True)
db.to_csv('output.csv',mode='a',header=True)  #add empty line
meandata.to_csv('output.csv',mode='a',header=True)
db.to_csv('output.csv',mode='a',header=True)  #add empty line
mediandata.to_csv('output.csv',mode='a',header=True)
db.to_csv('output.csv',mode='a',header=True)  #add empty line
modedata.to_csv('output.csv',mode='a',header=True)
Best Regards,
Sandeep

GANGA SANDEEP KUMAR