0

I have an excel file that contains the names of 60 datasets.

I'm trying to write a piece of code that "enters" the Excel file, accesses a specific dataset (whose name is in the Excel file), gathers and analyses some data and finally, creates a new column in the Excel file and inserts the information gathered beforehand.

I can do most of it, except for the part of adding a new column and entering the data.

I was trying to do something like this:

path_data = **the path to the excel file**

recap = pd.read_excel(os.path.join(path_data,'My_Excel.xlsx')) # where I access the Excel file

recap['New information Column'] = Some Value

Is this a correct way of doing this? And if so, can someone suggest a better way (that works ehehe)

Thank you a lot!

2
  • pandas.pydata.org/docs/reference/api/… Commented Apr 28, 2022 at 12:38
  • Thank you! I've been reading these files for quite some time now, and still can't figure out how to make it work! Guess I'm going back to the basics! Commented Apr 28, 2022 at 12:54

1 Answer 1

1

You can import the excel file into python using pandas.

import pandas as pd

df = pd.read_excel (r'Path\Filename.xlsx')
print (df)

If you have many sheets, then you could do this:

import pandas as pd

df = pd.read_excel (r'Path\Filename.xlsx', sheet_name='sheetname')
print (df)

To add a new column you could do the following:

df['name of the new column'] = 'things to add'

Then when you're ready, you can export it as xlsx:

import openpyxl

# to excel
df.to_excel(r'Path\filename.xlsx')
Sign up to request clarification or add additional context in comments.

2 Comments

This is exactly what I was looking for! I also forgot that when I import the Excel file with pd.read_excel it imports to python as a DataFrame! And thus, it is much easier now! Thank you very much!!
Great! I'm glad that this was useful :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.