- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我很难使用 pd.read_excel()
读取 Excel 工作表。
我的 Excel 表格的原始形式如下所示:
我希望数据框看起来像这样:
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]
添加到read_excel
,因为 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]
关于excel - 通过 pd.read_excel() 将 excel 工作表读取为多索引数据框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35300099/
我最近一直在学习 Clojure。 Clojure 世界中是否有类似 Scala 的工作表这样的东西,我可以在其中放入任何代码并在保存后立即对其进行评估?或者也许 Clojure 有类似的解决方案?
有人可以帮我吗?我想知道如何过滤工作表中的多个选项卡(C1-C19)。这是我所做的: 我创建了一张表格,将所有回复存储在我的谷歌表单(事件注册表单)中。每个参与者将收到一个坦克编号,每个坦克编号根据其
这就是我将打开的面板显示为 float 窗口的方式。 有人可以帮我将面板作为工作表运行吗?窗口对象是mWindow。我使用的许多标准代码都已被折旧。 NSOpenPanel *openPanel =
当您仅键入 worksheets() 时,默认范围 ActiveWorkbook 或 ThisWorkbook 是什么?对于那些不了解这些区别的人来说,它们非常重要,尤其是在 Excel 2013 中
我有一个带有一些图表的 HTML 页面。我想要做的是编写一个加载 javascript 函数,它将从 excel 表中读取值,将它们存储在变量中并在 html 页面上使用它们。我的问题是是否有任何 j
我需要将参数 callFrom 传递给 SwiftUI 中的工作表。 奇怪的是,该参数在第一次调用时没有使用,但对以下调用有效。 import SwiftUI struct ContentView:
我试着 var tempSheet = wrksheets[sheetName] as Worksheet; 在哪里 wrksheets是类型表 sheetName 是“带空格的工作表名称” 如果
该函数用作“ Assets 类别分配”引擎(在参数范围内具有约束)并在数组的每一行上模拟投资组合模型。我尝试使用四种方法将数组发布到工作表上,但每种方法都失败了。 对于 Assets A、B、C、D
目前,我的 excel 文件有两张表,一张名为“English”,一张名为“French”。 我以编程方式打开我的工作簿并编辑我的英文表,没有任何问题。当我打开第二张工作表时,出现以下错误: The
我添加了一个 VBA 表单 userform和一个模块 Module1在 Excel 中打开 Microsoft VBA 编辑器 (Alt+F11)。 现在,每当我打开任何其他 Excel 时,按 A
在单个 Excel 工作簿中,我想选择各种工作表来运行 VBA 子例程。我找到了显示如何遍历选定工作表的代码,它使用“MsgBox sh.Name”;但是,当我将代码放入其中时,它只会影响选择的最后一
我想知道是否有一个函数可以在 Excel 中加载特定于 Python 的工作表,例如,如果我有 34 张工作表只加载前 25 张工作表。通过以下行,我加载了所有工作表。 xlsx=pd.ExcelFi
我有一个名为“A”、“B”、“C”等的工作表的 xlsx。我需要形成一个名称为“A”、“B”、“C”的表作为第一列,以及来自的一些数据每个工作表中与第二列相同的单元格。例如,这可能看起来像: S
我有一张用密码保护的工作表。当我使用 VBA 更改该表上的任何内容时,我会像这样取消保护: Private Sub Worksheet_Change(ByVal target As Range)
我想将 Excel 文档插入 Excel 工作表。我可以通过以下步骤手动执行此操作; 插入/文本/对象/从文件创建(勾选显示为图标)/浏览。 然后我选择文件并插入文档。 我想通过宏来做到这一点。 (录
是否可以创建 批处理文件那将执行以下操作? 重命名 Excel 文件中的单个工作表(不是 Excel 工作簿/文件本身) 将简单格式应用于 Excel 文件 - 例如,将字体和字体大小应用于整个工作簿
Private Sub CommandButton1_Click() Dim ws As Worksheet With Application.FileDialog(msoFileDialog
我想知道是否可以在不复制该工作表的情况下引用另一本工作簿中的 Excel 工作表? 情况:我有一些非常大的工作表,其中充满了各种数据,但我不想在我的工作簿中保留它们的副本,因为虽然每个工作簿都使用相同
我有这个 Python 字典,我想将这个数据写入 Excel 文件。 注意:有很多类别,每个类别有很多汽车(为简单起见,我使用了 2 个类别) data = {"Category": {"Diesel
我有一个 excel 工作簿,在工作簿中我有 2 张名为 Front Page 和 Drafting 的工作表。起草工作表引用了首页工作表中的一些值。这只是一个基本的引用 我有像这样的公式:='Fro
我是一名优秀的程序员,十分优秀!