>>> df
A B C D E
0 one A foo 0.945847 -0.561259
1 one A foo 0.579520 0.130518
2 one A foo -0.683629 -1.084639
3 one A bar -0.168223 -0.311991
4 one B bar 0.007965 1.108121
5 one B bar -1.877323 -0.258055
6 one B bar 0.992160 0.192339
7 one B foo -0.421557 -0.805156
8 two C bar -0.346622 1.335197
9 two C foo -0.979483 -1.382465
10 two C bar -0.815332 -1.491385
11 two C foo -2.112730 -0.331574
>>> cols = ["A", "B", "C"]
>>> ndf = df.set_index(cols)
>>> ndf
D E
A B C
one A foo 0.945847 -0.561259
foo 0.579520 0.130518
foo -0.683629 -1.084639
bar -0.168223 -0.311991
B bar 0.007965 1.108121
bar -1.877323 -0.258055
bar 0.992160 0.192339
foo -0.421557 -0.805156
two C bar -0.346622 1.335197
foo -0.979483 -1.382465
bar -0.815332 -1.491385
foo -2.112730 -0.331574
ndf现在是一个多索引帧
为了使D和E与A、B和C处于同一级别,我们可以将索引设置为所有这些索引,以便于显示:
the_df = df.set_index(["A", "B", "C", "D", "E"])
获取(例如,在IPython笔记本中)
请注意,如果要在控制台中查看此项:
>>> the_df
Empty DataFrame
Columns: []
Index: [(one, A, foo, 0.945847, -0.561259), (one, A, foo, 0.57952, 0.130518), ...]
>>> the_df[""] = ""
>>> the_df
A B C D E
one A foo 0.945847 -0.561259
0.579520 0.130518
-0.683629 -1.084639
bar -0.168223 -0.311991
B bar 0.007965 1.108121
-1.877323 -0.258055
0.992160 0.192339
foo -0.421557 -0.805156
two C bar -0.346622 1.335197
foo -0.979483 -1.382465
bar -0.815332 -1.491385
foo -2.112730 -0.331574
删除HTML中额外的第一行:
from bs4 import BeautifulSoup
# form the soup
soup = BeautifulSoup(the_df.to_html())
# find the first row and remove it
soup.find("tr").extract()
# get HTML back
html = str(soup)
我想你在找
set_index
:使用一些示例数据:
ndf
现在是一个多索引帧为了使
D
和E
与A
、B
和C
处于同一级别,我们可以将索引设置为所有这些索引,以便于显示:获取(例如,在IPython笔记本中)
请注意,如果要在控制台中查看此项:
因为我们将所有内容都设置为索引,而值中没有任何内容!但是如果您也想在控制台中看到它,一个技巧是使用“ghost”列,即名称和值为空字符串
""
:删除HTML中额外的第一行:
相关问题 更多 >
编程相关推荐