Home > Blockchain >  Inserting Data into an Excel file using Pandas - Python
Inserting Data into an Excel file using Pandas - Python

Time:04-29

I have an excel file that contains the names of 60 datasets. Im 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 finnaly, creates a new collumn in the Excel file and inserts the information gathered beforehand.

I can do most of it, except for the part of adding a new collumn 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 Collumn'] = 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!

CodePudding user response:

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')
  • Related