0

I have the folowing dataframe in pandas. The date are two diffrent dates, in this case the 16th and 17th Time will go from 00:00 to 24:00 after that it will be the next day :-) And percentage will go from 100 to 0.

    date        time      percentage
   2018-08-16  00:00      36   
   2018-08-16  00:01      37   
   2018-08-16  00:01      38   
   2018-08-16  00:03      39   
   2018-08-16  00:03      40   
   2018-08-16  00:04      41   
   2018-08-16  00:05      42   
   2018-08-16  00:05      43   
   2018-08-16  00:06      44   
   2018-08-16  00:07      45   
   2018-08-16  00:07      46   
   2018-08-16  00:08      47
...
   2018-08-17  07:24      95   
   2018-08-17  07:25      94   
   2018-08-17  07:25      94   
   2018-08-17  07:32      95   
   2018-08-17  07:43      96   
   2018-08-17  07:52      97  
...

Now i would like to plot this dat in a line graph like this:

enter image description here

Tried something with:

df.set_index('date', inplace=True)
df.groupby('time')['percentage'].plot(legend=True)

plt.show()

But alway retrurn with a "TypeError: Empty 'DataFrame': no numeric data to plot"

Can somebody help me?

1
  • 2
    What is the dtype of 'percentage'? df['percentage'] = pd.to_numeric(df['percentage']) Commented Mar 29, 2019 at 18:36

2 Answers 2

1

You can try this:

df.set_index([df.groupby(['date','time']).cumcount(), 'date', 'time'])['percentage']\
  .unstack('date').reset_index(0, drop=True).sort_index().plot()

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

0

This should do:

df.percentage=df.percentage.astype(int)
for i in df.date.unique():
    aux=df[df.date==i]
    plt.plot(aux.time,aux.percentage)
plt.show()

6 Comments

Still get the error "TypeError: Empty 'DataFrame': no numeric data to plot"
Probably percentage isn't numeric. Can you check if it is being displayer as "30%" instead of 30 or 0.3 ?
If i print the dateframe they are like 36, 37, 38, 39 etc. when using "df = df.convert_objects(convert_numeric=True)" it work but it comes with a warning "FutureWarning: convert_objects is deprecated"
Try what I posted with the edited version and tell me if it works, please
Jeah superb! one question is it possible to set a tomestamp per hour on the x-as, at this moment it show "time" would like to get it per hour?
|

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.