为单行指定多个值

2024-06-02 07:57:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个熊猫数据框

^{tb1}$

我想把它转换成以下格式

^{tb2}$

我无法正确地显示它,但我不希望它再次重复类似的内容,而是将它们做成一个大框,并显示不常见的内容。我尝试使用多索引,但不起作用

我想要类似的东西

enter image description here

但在这里,它只是用一列来做,我希望它用我的用户名,用户名,信誉,答案的数量,问题的数量来做


Tags: 数据答案内容数量格式用户名信誉tb2
1条回答
网友
1楼 · 发布于 2024-06-02 07:57:28

我想你在找set_index

cols = ["USER_ID", "USER_NAME", "USER_REPUTATION", "NUMBER_OF_ANSWERS", "NUMBER_OF_QUESTIONS"]

ndf = df.set_index(cols)

使用一些示例数据:

>>> 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现在是一个多索引帧


为了使DEABC处于同一级别,我们可以将索引设置为所有这些索引,以便于显示:

the_df = df.set_index(["A", "B", "C", "D", "E"])

获取(例如,在IPython笔记本中)

enter image description here

请注意,如果要在控制台中查看此项:

>>> the_df

Empty DataFrame
Columns: []
Index: [(one, A, foo, 0.945847, -0.561259), (one, A, foo, 0.57952, 0.130518), ...]

因为我们将所有内容都设置为索引,而值中没有任何内容!但是如果您也想在控制台中看到它,一个技巧是使用“ghost”列,即名称和值为空字符串""

>>> 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)

相关问题 更多 >