【问题标题】:removing the name of a pandas dataframe index after appending a total row to a dataframe将总行附加到数据框后删除熊猫数据框索引的名称
【发布时间】:2015-12-26 09:23:31
【问题描述】:

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

我已将totalspt 数据框的index.name 设置为无。

但是,当数据框显示默认的 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

【问题讨论】:

    标签: python pandas indexing pivot-table


    【解决方案1】:

    您可以使用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
    

    【讨论】:

      【解决方案2】:

      这是列的名称。

      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
      

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

      In [15]: df.columns.name = None
      
      In [16]: df
      Out[16]:
         A  B
      0  1  2
      

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

      In [21]: df.columns.name = "XX"
      
      In [22]: df.index.name = "index"
      
      In [23]: df
      Out[23]:
      XX     A  B
      index
      0      1  2
      

      【讨论】:

        猜你喜欢
        • 2020-01-04
        • 2018-01-02
        • 2019-10-22
        • 2023-02-25
        • 2015-06-13
        • 1970-01-01
        • 2019-01-14
        • 2015-10-22
        • 2018-02-08
        相关资源
        最近更新 更多