MSAxes
MSAxes

Reputation: 15

How to write into Excel using Pandas?

The first code below works and allows me to read my excel file, as a test, I added a new column.

import numpy as np
import pandas as pd

excl = pd.read_excel (r'C:\Users\Family\Desktop\Anaconda\Book1.xlsx')
excl['new_pop'] = (excl.Pop2010 + excl.Pop2020)
print (excl)

Now, I would like to write the older data with the new column to a new excel file. Based on my online web research, I know I need to use the "to_excel" function in Pandas but I can't seem to find an example of how to bring over 'excl' file with the new data.

I previously had State, Country, Pop2010, Date, Pop2020 as columns, I added new_pop as a new column with new information in the cells beneath it.

Upvotes: 1

Views: 166

Answers (2)

konnichiwa
konnichiwa

Reputation: 60

You can make use of the 'to_excel' method. In your case, it would look like this:

excel.to_excel('name of file.xlsx')

For more on the to_excel method, here's the pandas documentation.

Upvotes: 0

programmar
programmar

Reputation: 654

excl.to_excel("new-file.xlsx")

Scroll down until you see examples: https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.to_excel.html

Examples are always documented in the pandas doc.

Upvotes: 1

Related Questions