0

i have a dataframe, and after grouping it with this code, i got the following dataframe

p7 = df.groupby(by=['interval','week_day']).size().unstack()

p7.reset_index(inplace=True)

p7['interval'] = pd.Categorical(p7['interval'], ["0:00", "0:30", "1:00","1:30",
                                                     "2:00","2:30","3:00","3:30","4:00",
                                                     "4:30","5:00","5:30","6:00", "6:30",
                                                     "7:00","7:30","8:00","8:30","9:00",
                                                     "9:30","10:00","10:30","11:00","11:30",
                                                       "12:00", "12:30", "13:00","13:30",
                                                       "14:00","14:30","15:00","15:30",
                                                       "16:00","16:30","17:00","17:30",
                                                       "18:00","18:30","19:00","19:30",
                                                       "20:00","20:30","21:00","21:30",
                                                       "22:00","22:30","23:00","23:30"])



p7 = p7.sort_values(by='interval',
                    ascending=True)

print(p7)

This is the output dataframe(p7)

enter image description here

But when I try to do a heatmap with this code, I got the following mistake:

plt.figure(figsize=(15,12))
sns.heatmap(p7,cmap="Greens")
plt.show()



ValueError: could not convert string to float: '0:00'

1 Answers1

1

Use:

del p7['interval']

or:

p7 = p7.drop('interval')

Or, if you want set interval as index:

p7 = p7.set_index('interval')

Based on the comment, the other option is to make a new df or directly send p7[p7.columns[1:]] to heatmap:

new_p7 = p7[p7.columns[1:]]

Probably you can directly send p7.drop('interval').

keramat
  • 4,328
  • 6
  • 25
  • 38
  • 1
    The first two options delete the interval data from the `df` and that is probably not what OP wants. Just don't pass these values to the `heatmap` func, no need to lose data. – rafaelc Jun 13 '22 at 03:59