1

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.

MSAxes
  • 15
  • 6
  • Does this answer your question? [how to save a pandas DataFrame to an excel file?](https://stackoverflow.com/questions/55170300/how-to-save-a-pandas-dataframe-to-an-excel-file) – Trenton McKinney Dec 24 '20 at 02:47

2 Answers2

1
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.

programmar
  • 594
  • 6
  • 19
0

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.

konnichiwa
  • 50
  • 6