Moustafa
Moustafa

Reputation: 11

Excel using Python

I'm new to Python and I'm trying to make a program that automate some daily Excel work.

I just want to copy data from a sheet to another using pandas but I got an error. Can anyone help?

import pandas as pd

File1 = pd.read_excel('FileName1.xlsx', sheet_name='Sheet1')
print(File1.columns)
print(File1['Date'][2])
File2 = pd.read_excel('FileName2.xlsx', sheet_name='Sheet2')
File1['Date'][0] = File2['Date'][0]

Here's the error:

SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame

Upvotes: 1

Views: 89

Answers (1)

Kevin K.
Kevin K.

Reputation: 1397

Your error is being thrown in the statement File1['Date'][0] = File2['Date'][0]. Pandas does not allow assignment using indexes like typical lists. Try using indexing with loc: File1.loc[0, 'Date'] = File2.loc[0, 'Date']

Upvotes: 1

Related Questions