팬더를 사용하여 python에서 Excel 파일 읽기
이런 식으로 엑셀 파일을 읽으려고 합니다.
newFile = pd.ExcelFile(PATH\FileName.xlsx)
ParsedData = pd.io.parsers.ExcelFile.parse(newFile)
두 개의 인수가 예상된다는 오류가 발생합니다. 두 번째 인수가 무엇인지 알 수 없으며, 여기서 달성하고자 하는 것은 Excel 파일을 DataFrame으로 변환하는 것입니다. 올바른 방법입니까?아니면 판다를 이용해서 이것을 하는 다른 방법이 있습니까?
닫기: 먼저 전화하십시오.ExcelFile
하지만 당신은 그 다음에 전화를 합니다..parse
시트 이름을 전달합니다.
>>> xl = pd.ExcelFile("dummydata.xlsx")
>>> xl.sheet_names
[u'Sheet1', u'Sheet2', u'Sheet3']
>>> df = xl.parse("Sheet1")
>>> df.head()
Tid dummy1 dummy2 dummy3 dummy4 dummy5 \
0 2006-09-01 00:00:00 0 5.894611 0.605211 3.842871 8.265307
1 2006-09-01 01:00:00 0 5.712107 0.605211 3.416617 8.301360
2 2006-09-01 02:00:00 0 5.105300 0.605211 3.090865 8.335395
3 2006-09-01 03:00:00 0 4.098209 0.605211 3.198452 8.170187
4 2006-09-01 04:00:00 0 3.338196 0.605211 2.970015 7.765058
dummy6 dummy7 dummy8 dummy9
0 0.623354 0 2.579108 2.681728
1 0.554211 0 7.210000 3.028614
2 0.567841 0 6.940000 3.644147
3 0.581470 0 6.630000 4.016155
4 0.595100 0 6.350000 3.974442
사용자가 하고 있는 것은 인스턴스가 아닌 클래스 자체에 상주하는 메서드를 호출하는 것입니다. 이는 괜찮지만(별로 관용적이지는 않지만), 그렇게 하는 경우에는 시트 이름도 전달해야 합니다.
>>> parsed = pd.io.parsers.ExcelFile.parse(xl, "Sheet1")
>>> parsed.columns
Index([u'Tid', u'dummy1', u'dummy2', u'dummy3', u'dummy4', u'dummy5', u'dummy6', u'dummy7', u'dummy8', u'dummy9'], dtype=object)
이것은 매우 간단하고 쉬운 방법입니다.
import pandas
df = pandas.read_excel(open('your_xls_xlsx_filename','rb'), sheetname='Sheet 1')
# or using sheet index starting 0
df = pandas.read_excel(open('your_xls_xlsx_filename','rb'), sheetname=2)
향후 경고:그sheetname
키워드는 최신 Pandas 버전에서는 더 이상 사용되지 않습니다. 사용sheet_name
대신.
여기에 추가해야 할 것이 있습니다. 행이나 열에 액세스하여 루프를 추가해야 합니다.
import pandas as pd
# open the file
xlsx = pd.ExcelFile("PATH\FileName.xlsx")
# get the first sheet as an object
sheet1 = xlsx.parse(0)
# get the first column as a list you can loop through
# where the is 0 in the code below change to the row or column number you want
column = sheet1.icol(0).real
# get the first row as a list you can loop through
row = sheet1.irow(0).real
편집:
방법들icol(i)
그리고.irow(i)
지금은 더 이상 사용되지 않습니다.사용할 수 있습니다.sheet1.iloc[:,i]
i-coland를 따다sheet1.iloc[i,:]
아이스로를 얻는 것.
이것이 당신의 요구를 충족시켜 줄 것이라고 생각합니다.
import pandas as pd
# Read the excel sheet to pandas dataframe
df = pd.read_excel("PATH\FileName.xlsx", sheet_name=0) #corrected argument name
여기 파이썬 코드에서 더 일반적인 구문으로 업데이트된 방법이 있습니다.또한 동일한 파일을 여러 번 열 수 없습니다.
import pandas as pd
sheet1, sheet2 = None, None
with pd.ExcelFile("PATH\FileName.xlsx") as reader:
sheet1 = pd.read_excel(reader, sheet_name='Sheet1')
sheet2 = pd.read_excel(reader, sheet_name='Sheet2')
https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.read_excel.html
파일의 경로를 입력하기만 하면 됩니다.pd.read_excel
import pandas as pd
file_path = "./my_excel.xlsx"
data_frame = pd.read_excel(file_path)
다음과 같은 매개 변수를 살펴보려면 설명서를 확인하십시오.skiprows
엑셀을 로드할 때 행을 무시함
import pandas as pd
data = pd.read_excel (r'**YourPath**.xlsx')
print (data)
시트 이름을 명시적으로 지정하지 않고 대신 시트 주문 번호를 지정하는 Excel 파일을 로드하는 것은 다음과 같습니다(대부분 첫 번째 시트를 로드합니다).
import pandas as pd
myexcel = pd.ExcelFile("C:/filename.xlsx")
myexcel = myexcel.parse(myexcel.sheet_names[0])
부터.sheet_names
시트 이름 목록을 반환합니다. 목록 요소를 호출하기만 하면 하나 이상의 시트를 쉽게 로드할 수 있습니다.
이 모든 것이 나를 위해 작동합니다.
In [1]: import pandas as pd
In [2]: df = pd.read_excel('FileName.xlsx') # If there is only one sheet in the excel file
In [3]: df = pd.read_excel('FileName.xlsx', sheet_name=0)
In [4]: In [20]: df = pd.read_excel('FileName.xlsx', sheet_name='Sheet 1')
#로드 판다 도서관
팬더를 PD로 수입합니다.
#파일이 있는 곳에 경로 설정
경로 = "//myfile.xlsx"
#데이터 프레임에 파일을 로드합니다.
df = pd.read_message(경로)
#처음 5개 행 확인
df.head(5)
언급URL : https://stackoverflow.com/questions/17063458/reading-an-excel-file-in-python-using-pandas
'programing' 카테고리의 다른 글
어떻게 하면 과거의 약속을 쉽게 고칠 수 있습니까? (0) | 2023.06.25 |
---|---|
레일에서 활성 레코드의 기본 표준 시간대를 변경하는 방법은 무엇입니까? (0) | 2023.06.25 |
MS SQL Server의 형식 번호(백분율) (0) | 2023.06.25 |
팬더 시리즈를 데이터 프레임으로 변환 (0) | 2023.06.25 |
여러 컨트롤러를 사용하는 Spring Boot API? (0) | 2023.06.25 |