Python Forum
help with exporting functions to excel
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
help with exporting functions to excel
#1
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
Reply
#2
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Exporting data frame to excel dyerlee91 0 1,606 Oct-05-2021, 11:34 AM
Last Post: dyerlee91

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020