Home > Mobile >  plot histogram with pandas with a time column as object?
plot histogram with pandas with a time column as object?

Time:09-03

I'm new in python. I would like to plot in a histogram the numbers of movements that made a group of insects in a determinated time. I have this dataframe:

 time     movements
00:00      3147
00:01      1590
00:02       450
......     .....
......     .....
......     .....
23:58      5432
23:59      5890 

'time' column is the time in a format hh:mm; and 'movements' column are the movements. I would like to make an histogram to know the numbers of movements that made the insects in differents intervals of time, for example: the number of movements made in a second, in 10 seconds, etc.

When I try build buckets of the time column, with the next line:

data['bucket'] = pd.cut(data['time'], 10)

I had this error:

TypeError: can only concatenate str (not "float") to str

My code is this:

# Read file
data=pd.read_csv(r'database.csv', low_memory=False)
data.head(2)

#Put the 'time' in 
data['bucket'] = pd.cut(data.time, 10)

#Using  groupby() method and sum() to add up the 'movements':
newdata = data[['bucket','movements']].groupby('bucket').sum()
print newdata

#Plotting
newdata.plot(kind='bar')

The error is because 'time' column is object, so, I have reading but I don't understand how to resolve it, I'm consufing and I would like that you can help me to see my error in my code. Thanks a lot.

Best regards

CodePudding user response:

You can convert the time from string to pd.Timedelta:

buckets = pd.cut(pd.to_timedelta(data['time']   ':00'), 10)
newd_data = data['movements'].groupby(buckets).sum()
  • Related