Python Forum
How to export from Python to Excel?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to export from Python to Excel?
#1
Hi everyone, I found the example below online that creates an excel file for you. I am currently working with a workbook in Python (using read_excel function), I added columns to it and edited some of the data. How would I export the new information to excel? Where would I add that to the code below?

excl = pd.DataFrame([['a', 'b'], ['c', 'd']],
                    index=['row 1', 'row 2'],
                    columns=['col 1', 'col 2'])
excl.to_excel("output.xlsx")
This is the code I used to rad the excel file:

excl = pd.read_excel (r'C:\Users\Family\Desktop\Anaconda\Book1.xlsx')
and the column names - State, Country
Pop2010, Date and Pop2020.
Reply
#2
Can you explain further? You have the command to export to excel already, line 4 of the first code snippet. Not sure of your question.
Reply
#3
(Dec-23-2020, 05:37 AM)jpy Wrote: Hi everyone, I found the example below online that creates an excel file for you. I am currently working with a workbook in Python (using read_excel function), I added columns to it and edited some of the data. How would I export the new information to excel? Where would I add that to the code below?

excl = pd.DataFrame([['a', 'b'], ['c', 'd']],
                    index=['row 1', 'row 2'],
                    columns=['col 1', 'col 2'])
excl.to_excel("output.xlsx")

Pandas lets you write Excel. After modifying or creating a new data frame, just call the to_excel() function again on that data frame.

Something like this:
import pandas as pd

# create test
excl = pd.DataFrame([['a', 'b'], ['c', 'd']],
                    index=['row 1', 'row 2'],
                    columns=['col 1', 'col 2'])
excl.to_excel("output.xls")

# now read and modify
excl = pd.read_excel("output.xls")
excl['State'] = ['a','b']
excl['Country'] = ['a','b']
excl['Pop2010]'] = ['a','b']
excl['Date'] = ['a','b']
excl['Pop2020'] = ['a','b']

# write dataframe
excl.to_excel("example.xlsx")
jpy likes this post
Reply
#4
There is no "export to excel". You can read and write Excel format files. If you read a file, modify the data, and write the data back to the same file descriptor it will look like you "modified" the file, but you really just replaced the file. To see any changes from Excel you need to load the file (in Excel) after the changes are made.
Reply
#5
Codeto, this is exactly what I was looking for, thank you.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  export into excel, how to implement pandas into for-loop deneme2 6 2,443 Sep-01-2022, 05:44 AM
Last Post: deneme2
Question Export Python output to Excel skyline1397 1 2,034 Jun-26-2022, 05:10 AM
Last Post: skyline1397
  How to keep columns header on excel without change after export data to excel file? ahmedbarbary 0 1,161 May-03-2022, 05:46 PM
Last Post: ahmedbarbary
  Skeleton file export error Python Code pepapoha 4 3,484 Nov-17-2020, 02:06 AM
Last Post: pepapoha
  Python Export Question Below samlee916 1 1,570 Jul-07-2020, 12:22 PM
Last Post: Larz60+
  CGI in python, problem with pandas, plotly.express and export html HK2432 0 2,124 Jan-19-2020, 01:30 PM
Last Post: HK2432
  export PDF to excel or csv prem2179 1 8,210 Jan-19-2019, 05:43 PM
Last Post: Larz60+
  export sql output to excel Kranthi 6 7,786 Oct-17-2018, 02:01 AM
Last Post: Kranthi
  python export to csv writes extra line between rows jahjahcity 4 10,341 Jul-25-2018, 01:36 AM
Last Post: jahjahcity
  [PYTHON EXPORT SQL TO .txt file] PYTHONDUDE 9 24,242 Feb-21-2018, 08:54 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

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