Python Forum
Pandas - Write to Exisitng Excel File - Sorted List
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
Reply
#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()
Reply
#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
Reply
#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.
Reply
#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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Pandas keep existing format of Excel AlphaInc 2 881 Jan-11-2024, 03:44 AM
Last Post: plonkarchivist
  SORTED.group by and count average of two columns [ sum of col 1 / sum of col 2 ] BSDevo 1 637 Oct-23-2023, 09:47 PM
Last Post: BSDevo
  Write from dictionary to excel divon 3 3,468 Jun-11-2023, 10:37 AM
Last Post: Larz60+
  Data Sorting and filtering(From an Excel File) PY_ALM 0 1,012 Jan-09-2023, 08:14 PM
Last Post: PY_ALM
Smile How to further boost the data read write speed using pandas tjk9501 1 1,228 Nov-14-2022, 01:46 PM
Last Post: jefsummers
  Split excel file and write output at specific row and set sheet position DSCA 0 1,958 May-12-2022, 07:29 PM
Last Post: DSCA
  How to import an xml file to Pandas sjhazard 0 2,324 Jun-08-2021, 08:19 PM
Last Post: sjhazard
  [Pandas] Write data to Excel with dot decimals manonB 1 5,774 May-05-2021, 05:28 PM
Last Post: ibreeden
  Comparing results within a list and appending to pandas dataframe Aryagm 1 2,320 Dec-17-2020, 01:08 PM
Last Post: palladium
  Creating more than one excel File at once malvarez1976 0 1,791 Dec-15-2020, 02:04 AM
Last Post: malvarez1976

Forum Jump:

User Panel Messages

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