将总行追加到数据帧后删除pandas数据帧索引的名称

yos*_*rry 9 python indexing pivot-table pandas

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

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

但是,当数据帧显示默认的0,1,2,3索引时,它不会在索引正上方的左上角显示默认的空单元格.

我怎么能在数据帧中使这个单元格为空?

在此输入图像描述

   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
Run Code Online (Sandbox Code Playgroud)

And*_*den 7

那是列的名字.

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
Run Code Online (Sandbox Code Playgroud)

您可以将其设置为"无"以清除它.

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

In [16]: df
Out[16]:
   A  B
0  1  2
Run Code Online (Sandbox Code Playgroud)

如果你想保留它,另一种方法是给索引一个名字:

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

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

In [23]: df
Out[23]:
XX     A  B
index
0      1  2
Run Code Online (Sandbox Code Playgroud)