Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

I have set the index.name for the totalspt dataframe to None.

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.

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
like image 734
yoshiserry Avatar asked Sep 29 '15 02:09

yoshiserry


People also ask

How can you remove an index name from your DataFrame in pandas?

Drop the index column of Pandas DataFrame We can remove the index column in existing dataframe by using reset_index() function. This function will reset the index and assign the index columns start with 0 to n-1.

How do I hide an index in a data frame?

Use hide_index() By using hide_index() method, you can hide the Index of the DataFrame.

How do I ignore index in Python?

Practical Data Science using Python To create a DataFrame from DateTimeIndex ignoring the index, use the DateTimeIndex. to_frame() method. Set the parameter index to False to ignore the index.


2 Answers

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.

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
like image 145
Andy Hayden Avatar answered Oct 06 '22 01:10

Andy Hayden


You can use rename_axis. Since 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
like image 22
Zero Avatar answered Oct 06 '22 03:10

Zero