简体   繁体   中英

Showing different data using dataframe.plot from pandas

I want to show some data using plots. My dataframe has 3 columns,

Id  Round Time
1   1     158
1   2     169
1   3     172
2   1     156
2   2     168
2   3     176

With this example what I want to do is show 2 lines in my plot (with different colors), each line must correspond to the id, the x axis with the number of the round and the y axis with time.

Now I'm doing this and it's obviously wrong,

import pandas as pd

data = [[1,1,158],[1,2,169],[1,3,172],
        [2,1,156],[2,2,168],[2,3,176]]
cols = ['Id','Round','Time']
df = pd.DataFrame(data, columns=cols)

df.plot(kind='line',x='Round',y='Time')
plt.show()

How can I differentiate each of the different ids?

Thank you very much!!

You need to create a pivot and then plot it.

In [49]: df.pivot('Round','Id','Time').plot(kind='line')
Out[49]: <matplotlib.axes._subplots.AxesSubplot at 0x2362f31ae10>

In [50]: plt.show()

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM