excel 시트를 다중 인덱스 데이터 프레임으로 읽기(pd.read_databrame())
엑셀 시트를 읽는 데 어려움을 겪고 있습니다pd.read_excel()
.
Excel 테이블은 raw 형태로 다음과 같습니다.
데이터 프레임은 다음과 같이 될 것으로 예상했습니다.
bar baz foo
one two one two one two
A B C D E F
baz one 0.085930 -0.848468 0.911572 -0.705026 -1.284458 -0.602760
two 0.385054 2.539314 0.589164 0.765126 0.210199 -0.481789
three -0.352475 -0.975200 -0.403591 0.975707 0.533924 -0.195430
이게 가능하기나 해?
실패한 시도:
xls_file = pd.read_excel(data_file, header=[0,1,2], index_col=None)
원시 Excel 파일에 대한 링크:
https://www.dropbox.com/s/ek646ab4yb1fvdq/ipsos_excel_tables_type_2_trimed_nosig.xlsx?dl=0
파라미터를 추가할 수 있습니다.index_col=[0,1]
에 대해서, 왜냐하면index
이Multindex
또, 다음과 같습니다.
편집:
당신도 변화가 필요합니다.header
부터header=[0,1,2]
로.header=[0,1]
및 빈 행을 삭제합니다.- 행5
그리고.7
. 파라미터를 추가할 수 있습니다.sheetname
:
import pandas as pd
df = pd.read_excel('test/ipsos_excel_tables_type_2_trimed_nosig.xlsx',
header=[0,1],
index_col=[0,1],
sheetname="0001")
print df
T \
Total
Q1. Do you have a social media account (such as... Unweighted base 2019
Weighted base 2019
Yes 1519
Yes 75
No 494
No 24
Don’t know 5
Gender \
Male
Q1. Do you have a social media account (such as... Unweighted base 1011
Weighted base 1000
Yes 705
Yes 70
No 291
No 29
Don’t know 4
\
Female
Q1. Do you have a social media account (such as... Unweighted base 1008
Weighted base 1019
Yes 814
Yes 80
No 204
No 20
Don’t know 1
Age \
16-24
Q1. Do you have a social media account (such as... Unweighted base 321
Weighted base 323
Yes 293
Yes 91
No 28
No 9
Don’t know 1
\
25-34
Q1. Do you have a social media account (such as... Unweighted base 361
Weighted base 361
Yes 318
Yes 88
No 41
No 11
Don’t know 2
\
35-44
Q1. Do you have a social media account (such as... Unweighted base 372
Weighted base 370
Yes 289
Yes 78
No 81
No 22
Don’t know -
\
45-54
Q1. Do you have a social media account (such as... Unweighted base 376
Weighted base 376
Yes 258
Yes 69
No 118
No 31
Don’t know -
\
55-75
Q1. Do you have a social media account (such as... Unweighted base 589
Weighted base 589
Yes 361
Yes 61
No 227
No 38
Don’t know 2
Social grade \
AB
Q1. Do you have a social media account (such as... Unweighted base 593
Weighted base 533
Yes 416
Yes 78
No 116
No 22
Don’t know 1
\
C1
Q1. Do you have a social media account (such as... Unweighted base 588
Weighted base 563
Yes 417
Yes 74
No 147
No 26
Don’t know -
... \
...
Q1. Do you have a social media account (such as... Unweighted base ...
Weighted base ...
Yes ...
Yes ...
No ...
No ...
Don’t know ...
Region (4 code scale) \
East
Q1. Do you have a social media account (such as... Unweighted base 341
Weighted base 342
Yes 259
Yes 76
No 83
No 24
Don’t know -
Education \
GCSE/O Level/NVQ12
Q1. Do you have a social media account (such as... Unweighted base 503
Weighted base 520
Yes 370
Yes 71
No 147
No 28
Don’t know 4
\
A Level or equivalent
Q1. Do you have a social media account (such as... Unweighted base 454
Weighted base 461
Yes 359
Yes 78
No 101
No 22
Don’t know 1
\
Degree/Masters/PhD
Q1. Do you have a social media account (such as... Unweighted base 914
Weighted base 886
Yes 697
Yes 79
No 189
No 21
Don’t know -
\
No formal qualifications
Q1. Do you have a social media account (such as... Unweighted base 148
Weighted base 152
Yes 93
Yes 61
No 58
No 38
Don’t know 1
Employment status \
Full-time
Q1. Do you have a social media account (such as... Unweighted base 774
Weighted base 763
Yes 598
Yes 78
No 162
No 21
Don’t know 3
\
Part-time
Q1. Do you have a social media account (such as... Unweighted base 272
Weighted base 274
Yes 195
Yes 71
No 79
No 29
Don’t know -
\
Self-Employed
Q1. Do you have a social media account (such as... Unweighted base 166
Weighted base 162
Yes 108
Yes 67
No 54
No 33
Don’t know -
\
ANY WORKING
Q1. Do you have a social media account (such as... Unweighted base 1212
Weighted base 1200
Yes 901
Yes 75
No 295
No 25
Don’t know 3
ANY NOT WORKING
Q1. Do you have a social media account (such as... Unweighted base 625
Weighted base 645
Yes 460
Yes 71
No 183
No 28
Don’t know 2
[7 rows x 25 columns]
언급URL : https://stackoverflow.com/questions/35300099/reading-excel-sheet-as-multiindex-dataframe-through-pd-read-excel
'programing' 카테고리의 다른 글
Flutter로 앱 표시명 빌드를 변경하려면 어떻게 해야 하나요? (0) | 2023.04.10 |
---|---|
의 줄바꿈으로 문자열을 분할합니다.그물 (0) | 2023.04.10 |
std:: wstring VS std:: string (0) | 2023.04.10 |
셀 내 줄바꿈 문자 추가(CSV) (0) | 2023.04.10 |
bash 스크립트에서 파일의 첫 줄을 가져오려면 어떻게 해야 합니까? (0) | 2023.04.10 |