Τμήμα Πληροφορικής και Τηλεπικοινωνιών - Άρτα
Πανεπιστήμιο Ιωαννίνων
Γκόγκος Χρήστος
http://chgogos.github.io/
Τελευταία ενημέρωση: 2/4/2022
Επεξεργασία δεδομένων που βρίσκονται σε αρχεία Excel.
Η ανάγνωση και εγγραφή αρχείων Excel γίνεται με τη χρήση επιλέον modules που πρέπει να εγκατασταθούν. Δείτε το www.python-excel.org.
Στα ακόλουθα παραδείγματα θα χρησιμοποιηθεί το openpyxl και το pandas.
import openpyxl
path = "../../../datasets/cal_housing.xlsx"
book = openpyxl.load_workbook(path)
all_sheets = book.get_sheet_names()
print(all_sheets)
sheet = book.active
print("*" * 40)
# διάσχιση κατά γραμμές (για τις 5 πρώτες γραμμές του φύλλου)
for row in sheet.iter_rows(min_row=1, max_row=5):
for cell in row:
print(cell.value, end=" ")
print()
print("*" * 40)
# διάσχιση κατά στήλες (στις 2 πρώτες στήλες, για τις 5 πρώτες γραμμές του φύλλου)
for row in sheet.iter_cols(min_row=1, max_row=5, min_col=1, max_col=2):
for cell in row:
print(cell.value, end=" ")
print()
['cal_housing'] **************************************** longitude latitude housingMedianAge totalRooms totalBedrooms population households medianIncome medianHouseValue -122.23 37.88 41 880 129 322 126 8.3252 452600 -122.22 37.86 21 7099 1106 2401 1138 8.3014 358500 -122.24 37.85 52 1467 190 496 177 7.2574 352100 -122.25 37.85 52 1274 235 558 219 5.6431 341300 **************************************** longitude -122.23 -122.22 -122.24 -122.25 latitude 37.88 37.86 37.85 37.85
C:\Users\chgogos\AppData\Local\Temp\ipykernel_39616\1282899620.py:5: DeprecationWarning: Call to deprecated function get_sheet_names (Use wb.sheetnames). all_sheets = book.get_sheet_names()
# χρειάζεται να εγκατασταθεί το module xlrd
import pandas as pd
path = "../../../datasets/cal_housing.xls"
xl = pd.ExcelFile(path)
print(xl.sheet_names)
df = xl.parse('california_housing')
print(df.info())
df
['california_housing'] <class 'pandas.core.frame.DataFrame'> RangeIndex: 16383 entries, 0 to 16382 Data columns (total 9 columns): # Column Non-Null Count Dtype --- ------ -------------- ----- 0 longitude 16383 non-null float64 1 latitude 16383 non-null float64 2 housingMedianAge 16383 non-null int64 3 totalRooms 16383 non-null int64 4 totalBedrooms 16383 non-null int64 5 population 16383 non-null int64 6 households 16383 non-null int64 7 medianIncome 16383 non-null float64 8 medianHouseValue 16383 non-null int64 dtypes: float64(3), int64(6) memory usage: 1.1 MB None
longitude | latitude | housingMedianAge | totalRooms | totalBedrooms | population | households | medianIncome | medianHouseValue | |
---|---|---|---|---|---|---|---|---|---|
0 | -122.23 | 37.88 | 41 | 880 | 129 | 322 | 126 | 8.3252 | 452600 |
1 | -122.22 | 37.86 | 21 | 7099 | 1106 | 2401 | 1138 | 8.3014 | 358500 |
2 | -122.24 | 37.85 | 52 | 1467 | 190 | 496 | 177 | 7.2574 | 352100 |
3 | -122.25 | 37.85 | 52 | 1274 | 235 | 558 | 219 | 5.6431 | 341300 |
4 | -122.25 | 37.85 | 52 | 1627 | 280 | 565 | 259 | 3.8462 | 342200 |
... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
16378 | -121.29 | 38.02 | 12 | 2006 | 426 | 1849 | 396 | 2.5437 | 99000 |
16379 | -121.30 | 38.02 | 16 | 2717 | 621 | 3343 | 643 | 2.5473 | 106300 |
16380 | -121.30 | 38.03 | 11 | 2866 | 654 | 1404 | 525 | 2.5050 | 95000 |
16381 | -121.30 | 38.02 | 4 | 1515 | 384 | 491 | 348 | 2.8523 | 87500 |
16382 | -121.29 | 38.00 | 4 | 1392 | 322 | 1784 | 309 | 2.3750 | 124500 |
16383 rows × 9 columns