Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Working with excel files
#2
using the excel file I have attached, you can read the data with pandas

This example shows opening xlsx file with two sheets
Using your file, You can use same method to read your data, select what you want and rewrite (see references at bottom):
import pandas as pd
from pathlib import Path
import os

# Set starting path
os.chdir(os.path.abspath(os.path.dirname(__file__)))
homepath = Path('.')
excel_file = homepath / '../data/excel/Data.xlsx'

exfile = pd.ExcelFile(excel_file)

df1 = pd.read_excel(exfile, 'ZipcodeData')
df2 = pd.read_excel(exfile, 'Geography')

print(f"\n{df1}\n")
print(f"\n{df2}\n")
Output:
CountyName CountySeat FipsCd LocalCountyId ZipCode 0 Boone Belvidere 7 4 61008 1 Boone Belvidere 7 4 61011 2 Boone Belvidere 7 4 61011 3 Boone Belvidere 7 4 61012 4 Carroll Mount Carroll 15 8 61014 5 Boone Belvidere 7 4 61038 6 Boone Belvidere 7 4 61038 7 Boone Belvidere 7 4 61038 8 Carroll Mount Carroll 15 8 61046 9 Carroll Mount Carroll 15 8 61046 10 Carroll Mount Carroll 15 8 61051 11 Carroll Mount Carroll 15 8 61053 12 Carroll Mount Carroll 15 8 61053 13 Boone Belvidere 7 4 61065 14 Boone Belvidere 7 4 61065 15 Boone Belvidere 7 4 61065 16 Carroll Mount Carroll 15 8 61074 17 Carroll Mount Carroll 15 8 61074 Zip City State Latitude Longitude Timezone Daylight savings time flag Latitude.1 Longitude.1 0 61008 Belvidere IL 42.257229 -88.84755 -6 1 42.257229 -88.84755 1 61011 Caledonia IL 42.376423 -88.93250 -6 1 42.376423 -88.93250 2 61012 Capron IL 42.401104 -88.74726 -6 1 42.401104 -88.74726 3 61014 Chadwick IL 41.970680 -89.87508 -6 1 41.970680 -89.87508 4 61038 Garden Prairie IL 42.255617 -88.74344 -6 1 42.255617 -88.74344 5 61046 Lanark IL 42.111421 -89.82436 -6 1 42.111421 -89.82436 6 61051 Milledgeville IL 41.965904 -89.75896 -6 1 41.965904 -89.75896 7 61053 Mount Carroll IL 42.100900 -89.98255 -6 1 42.100900 -89.98255 8 61065 Poplar Grove IL 42.364394 -88.84665 -6 1 42.364394 -88.84665 9 61074 Savanna IL 42.099635 -90.12858 -6 1 42.099635 -90.12858

.xlsx   Data.xlsx (Size: 7.08 KB / Downloads: 104)

References:
read pandas data from excel: https://pandas.pydata.org/docs/reference...excel.html
write padnas data to excel: https://pandas.pydata.org/docs/reference...excel.html
arsouzaesilva likes this post
Reply


Messages In This Thread
Working with excel files - by arsouzaesilva - Aug-24-2021, 06:13 PM
RE: Working with excel files - by Larz60+ - Aug-24-2021, 08:49 PM
RE: Working with excel files - by Pedroski55 - Aug-25-2021, 01:07 AM
RE: Working with excel files - by Larz60+ - Aug-25-2021, 03:12 AM
RE: Working with excel files - by Pedroski55 - Aug-25-2021, 07:23 AM
RE: Working with excel files - by Larz60+ - Aug-25-2021, 10:04 AM
RE: Working with excel files - by arsouzaesilva - Sep-17-2021, 06:52 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Excel isnt working properly after python function is started IchNar 2 350 May-01-2024, 06:43 PM
Last Post: IchNar
  Copy Paste excel files based on the first letters of the file name Viento 2 503 Feb-07-2024, 12:24 PM
Last Post: Viento
  Working with Excel and Word, Several Questions Regarding Find and Replace Brandon_Pickert 4 1,633 Feb-11-2023, 03:59 PM
Last Post: Brandon_Pickert
  How to loop through all excel files and sheets in folder jadelola 1 4,612 Dec-01-2022, 06:12 PM
Last Post: deanhystad
  Creating csv files from Excel file azizrasul 40 6,025 Nov-03-2022, 08:33 PM
Last Post: azizrasul
  win32com — How to resolve “AttributeError: xlUp” for Excel files? JaneTan 2 4,326 Aug-18-2021, 05:27 AM
Last Post: snippsat
Question Working with existing files Gilush 7 3,138 Feb-10-2021, 08:55 PM
Last Post: Gilush
  filecmp is not working for UTF-8 BOM encoded files sureshnagarajan 3 2,661 Feb-10-2021, 11:17 AM
Last Post: sureshnagarajan
  MP3 Tags to Excel Not working giddyhead 5 2,991 Jan-27-2021, 03:23 AM
Last Post: giddyhead
  code to read files in folders and transfer the file name, type, date created to excel Divya577 0 1,897 Dec-06-2020, 04:14 PM
Last Post: Divya577

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020