pandas 在将总行附加到数据帧后删除熊猫数据帧索引的名称

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32834785/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 23:56:30  来源:igfitidea点击:

removing the name of a pandas dataframe index after appending a total row to a dataframe

pythonpandasindexingpivot-table

提问by yoshiserry

I have calculated a series of totals tips by day of a week and appended it to the bottom of totalsptdataframe.

我已经按一周中的每一天计算了一系列总计提示,并将其附加到totalspt数据框的底部。

I have set the index.namefor the totalsptdataframe to None.

我已将数据框的设置index.nametotalspt无。

However while the dataframe is displaying the default 0,1,2,3 index it doesn't display the default empty cell in the top left directly above the index.

然而,当数据框显示默认的 0,1,2,3 索引时,它不会在索引正上方的左上角显示默认的空单元格。

How could I make this cell empty in the dataframe?

如何在数据框中使此单元格为空?

enter image description here

在此处输入图片说明

   total_bill   tip sex smoker  day time  size   tip_pct
0       16.54  1.01   F      N  Sun    D     2  0.061884
1       12.54  1.40   F      N  Mon    D     2  0.111643
2       10.34  3.50   M      Y  Tue    L     4  0.338491
3       20.25  2.50   M      Y  Wed    D     2  0.123457
4       16.54  1.01   M      Y  Thu    D     1  0.061064
5       12.54  1.40   F      N  Fri    L     2  0.111643
6       10.34  3.50   F      Y  Sat    D     3  0.338491
7       23.25  2.10   M      Y  Sun    B     3  0.090323

pivot  =  tips.pivot_table('total_bill', index=['sex', 'size'],columns=['day'],aggfunc='sum').fillna(0)
print pivot
day         Fri    Mon    Sat    Sun    Thu    Tue    Wed
sex size
F   2     12.54  12.54   0.00  16.54   0.00   0.00   0.00
    3      0.00   0.00  10.34   0.00   0.00   0.00   0.00
M   1      0.00   0.00   0.00   0.00  16.54   0.00   0.00
    2      0.00   0.00   0.00   0.00   0.00   0.00  20.25
    3      0.00   0.00   0.00  23.25   0.00   0.00   0.00
    4      0.00   0.00   0.00   0.00   0.00  10.34   0.00

totals_row  =  tips.pivot_table('total_bill',columns=['day'],aggfunc='sum').fillna(0).astype('float')
totalpt  = pivot.reset_index('sex').reset_index('size')
totalpt.index.name = None
totalpt = totalpt[['Fri', 'Mon','Sat', 'Sun', 'Thu', 'Tue', 'Wed']]
totalpt = totalpt.append(totals_row)
print totalpt
**day**              Fri    Mon    Sat    Sun    Thu    Tue    Wed #problem text day 
0           12.54  12.54   0.00  16.54   0.00   0.00   0.00
1            0.00   0.00  10.34   0.00   0.00   0.00   0.00
2            0.00   0.00   0.00   0.00  16.54   0.00   0.00
3            0.00   0.00   0.00   0.00   0.00   0.00  20.25
4            0.00   0.00   0.00  23.25   0.00   0.00   0.00
5            0.00   0.00   0.00   0.00   0.00  10.34   0.00
total_bill  12.54  12.54  10.34  39.79  16.54  10.34  20.25

采纳答案by Andy Hayden

That's the columns' name.

那是列的名称。

In [11]: df = pd.DataFrame([[1, 2]], columns=['A', 'B'])

In [12]: df
Out[12]:
   A  B
0  1  2

In [13]: df.columns.name = 'XX'

In [14]: df
Out[14]:
XX  A  B
0   1  2

You can set it to None to clear it.

您可以将其设置为 None 以清除它。

In [15]: df.columns.name = None

In [16]: df
Out[16]:
   A  B
0  1  2


An alternative, if you wanted to keep it, is to give the index a name:

如果您想保留它,另一种方法是为索引命名:

In [21]: df.columns.name = "XX"

In [22]: df.index.name = "index"

In [23]: df
Out[23]:
XX     A  B
index
0      1  2

回答by Zero

You can use rename_axis. Since 0.17.0

您可以使用rename_axis. 自从0.17.0

In [3939]: df
Out[3939]:
XX  A  B
0   1  2

In [3940]: df.rename_axis(None, axis=1)
Out[3940]:
   A  B
0  1  2

In [3942]: df = df.rename_axis(None, axis=1)

In [3943]: df
Out[3943]:
   A  B
0  1  2