-
Notifications
You must be signed in to change notification settings - Fork 0
/
unio.py
32 lines (23 loc) · 1.18 KB
/
unio.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import pandas as pd
# Load the Excel files
file1 = pd.ExcelFile('montanya.xlsx') # Workbook
file2 = pd.read_excel('municipis.xlsx') # Single sheet file
# Extract population data from the second file
population_data = file2.iloc[:, 1]
# Initialize an empty DataFrame for the output
output_data = pd.DataFrame()
# Iterate through each sheet in file1 starting from the second sheet
for sheet_name in file1.sheet_names[1:]:
# Extract altitude data from the current sheet
altitude_data = file1.parse(sheet_name=sheet_name, usecols=[1], header=None).squeeze()
# Check if municipalities match
municipalities = file1.parse(sheet_name=sheet_name, usecols=[0], header=None).squeeze()
# Create a DataFrame for the current sheet's output
sheet_output = pd.DataFrame({'Municipality': municipalities})
# Add altitude and population data
sheet_output['Altitude'] = altitude_data
sheet_output['Population'] = population_data
# Concatenate current sheet's output with overall output
output_data = pd.concat([output_data, sheet_output])
# Save the output to a new Excel file
output_data.to_excel('output.xlsx', index=False)