I have yearly separate excel files. I would like to load one year's file, use it to calculate a reduced file, save it and then do the same for the remaining years.

For example

import pandas as pd

list_years = ['1995_1996', '1996_1997','1997_1998']

for i in list_years:

    data1 = "data1" + i + ".xls"

    df1 = pd.read_xls(data1)

    data2 = "data2" + i + ".xls"

    df2 = pd.read_xls(data2) 

 df = pd.merge(df1, df2, on='A', how='left')

manipulate the 2 data files, for example,

df3 = df.groupby(['B', 'C'])['D'].mean().reset_index()

And save a the reduced df, df3, to excel.

save = "new_data" + i + ".xls"

df3.to_excel(save)

Then do the same for the others in the list.

When I run it this way it simply does the first entry in the list but then stops.

Any help would be appreciated!

0 Answers