My Pandas dataframe code
import pandas as pd
df = pd.DataFrame({'Impressions': [92964, 91282, 88143,272389], 'Clicks': [3128, 3131, 2580, 8839]}, index=pd.to_datetime(['6/1/2015', '6/8/2015', '6/15/2015', '1/1/2020']))
df.index.name = 'Date'
Produces
Clicks Impressions
Date
2015-06-01 3128 92964
2015-06-08 3131 91282
2015-06-15 2580 88143
2020-01-01 8839 272389
How can I change the 2020-01-01
to be a string that says Total
?
What I want to achieve is this:
Clicks Impressions
Date
2015-06-01 3128 92964
2015-06-08 3131 91282
2015-06-15 2580 88143
Total 8839 272389
More context
df.index.dtype
is of datatype dtype('<M8[ns]')
I think I can access the index row label by this df.index[-1]
which tells me it's a Timestamp('2020-01-01 00:00:00')
.
But if I try to do something like this, it does not work:
df.index[-1] = 'Total'
Error:
Traceback (most recent call last):
File "<pyshell#8>", line 1, in <module>
df.index[-1] = 'Total'
File "C:\Python34\lib\site-packages\pandas\core\index.py", line 922, in __setitem__
raise TypeError("Indexes does not support mutable operations")
TypeError: Indexes does not support mutable operations