2

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

1 Answer 1

1

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()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.