Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Pandas - Write to Exisitng Excel File - Sorted List
#1
hi friends

how can i write my sorted list to an exisitng excel file ie sheet name and column number

from openpyxl import load_workbook
import pandas as pd
from pandas import ExcelWriter
from pandas import ExcelFile
 
df = pd.read_excel('python.xlsx', sheet_name='Sheet1')

z= list(df.names)                                                    #Column Name is imported into a List  >  ['Sean', 'Joyce', 'Ruby', 'Pamela']

print(sorted(z,key=len))



#sorted_words=(sorted(z,key=len))


#sorted_words.to_excel('test.xlsx')

thank you for your help



:)


Python newbie trying to learn the ropes
Quote
#2
Well without seeing the error its hard to say what the problem is. I would start debugging by checking the type of what your trying to write to make sure its what to_excel() function is expecting. I am not sure what exactly it is you are trying to do. It seems as thought you are attempting to take the Column names from python.xlsx, sort them and write them to a different excel file; is that correct?

I have never used Pandas or messed with excel files before but I can tell you this much; to_excel() is expecting type ExcelWriter ie:

# Specify a writer
writer = pd.ExcelWriter('example.xlsx', engine='xlsxwriter')

# Write your DataFrame to a file     
yourData.to_excel(writer, 'Sheet1')

# Save the result 
writer.save()
Quote
#3
Hello,

thank you for your help

I included the new code
but an error came up

z.to_excel(writer, 'Sheet1')
AttributeError: 'list' object has no attribute 'to_excel'



Yes i wanted to output my sorted list into an excel file



:)


Python newbie trying to learn the ropes
Quote
#4
Sure, that error makes sense right because z is a list where to_excel() only works on ExcelWriter() type. So just pack it back into the proper type. The sooner you learn to start type checking and figuring out what the functions your attempting to use are expecting (in terms of type) the better.
Quote
#5
Hi,

is there an example that can show me how to type cast, i have not been able to find anything.
That applie to my example.
I sorted my list
I just want to write my list to excel sheet now



:)


Python newbie trying to learn the ropes
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Can't read text file with pandas zinho 6 3,126 May-24-2020, 06:13 AM
Last Post: azajali43
  how to solve the 'NO SUCH DIRECTORY OR FILE' in pandas, python MohammedSohail 10 560 May-08-2020, 07:45 AM
Last Post: nnk
  pandas.read_sas with chunksize: IndexError list index out of range axelle 0 266 Jan-28-2020, 09:30 AM
Last Post: axelle
  Convert Excel to .txt - Need each excel row to be separate .txt file cowboykevin05 2 826 Jan-03-2020, 06:29 PM
Last Post: Larz60+
  [split] Converting excel file to txt file unexceptionalhobby 2 1,262 Oct-16-2019, 06:34 PM
Last Post: unexceptionalhobby
  Read exel with merged cells and write to another excel SriMekala 0 886 Aug-10-2019, 07:14 AM
Last Post: SriMekala
  how read and write merged cells in excel SriMekala 1 3,757 Aug-07-2019, 11:27 PM
Last Post: scidam
  Need Help With Filtering Data For Excel Files Using Pandas eddywinch82 9 1,129 Aug-06-2019, 03:44 PM
Last Post: eddywinch82
  Python write result of VAR to excel file wissam1974 8 1,753 Jul-13-2019, 01:09 PM
Last Post: wissam1974
  How to use the excel filename as a value to populate new column, using Pandas? Dequanharrison 5 1,451 Jun-26-2019, 11:11 PM
Last Post: Dequanharrison

Forum Jump:


Users browsing this thread: 1 Guest(s)