Removing the pandas dataframe index name after adding a common row to the data frame

I calculated a series of summary tips for the days of the week and attached them to the bottom of the totalspt dataframe.

I set index.name for the index.name frame to None.

However, while the dataframe displays the default index 0,1,2,3, it does not display the default empty cell in the upper left corner above the index.

How can I make this cell empty in the data frame?

enter image description here

  total_bill tip sex smoker day time size tip_pct 0 16.54 1.01 FN Sun D 2 0.061884 1 12.54 1.40 FN Mon D 2 0.111643 2 10.34 3.50 MY Tue L 4 0.338491 3 20.25 2.50 MY Wed D 2 0.123457 4 16.54 1.01 MY Thu D 1 0.061064 5 12.54 1.40 FN Fri L 2 0.111643 6 10.34 3.50 FY Sat D 3 0.338491 7 23.25 2.10 MY 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 
+5
source share
2 answers

This is the name of the column.

 In [11]: df = pd.DataFrame([[1, 2]], columns=['A', 'B']) In [12]: df Out[12]: AB 0 1 2 In [13]: df.columns.name = 'XX' In [14]: df Out[14]: XX AB 0 1 2 

You can set it to No to clear it.

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

An alternative if you want to keep it is to name it:

 In [21]: df.columns.name = "XX" In [22]: df.index.name = "index" In [23]: df Out[23]: XX AB index 0 1 2 
+3
source

You can use rename_axis . Since 0.17.0

 In [3939]: df Out[3939]: XX AB 0 1 2 In [3940]: df.rename_axis(None, axis=1) Out[3940]: AB 0 1 2 In [3942]: df = df.rename_axis(None, axis=1) In [3943]: df Out[3943]: AB 0 1 2 
0
source

Source: https://habr.com/ru/post/1232495/


All Articles