简体   繁体   English

如何在折线图中绘制带有日期时间列的数据框?

[英]How to plot a dataframe with columns of datetimes in a line graph?

I have a dataframe that looks like this: 我有一个看起来像这样的数据框:

Index      Andy           Bobby        Charlie
0          2019-01-02     2019-01-01   2019-01-15
1          2019-01-05     2019-01-04   2019-01-17
2          2019-01-06     2019-02-03   2019-02-14
...
103        2019-07-01     2019-06-03   NaT
104        2019-07-06     NaT          NaT
105        2019-07-23     NaT          NaT

The dates are when each person had a meeting with a client. 日期是每个人与客户开会的日期。 I want to create a plot of line graphs that chart how many meetings each person had since year to date. 我想创建一个折线图,以图表显示自今年以来每个人举行了多少次会议。

I'm having trouble with the plotting syntax, also the NaT values are making it tough to graph every column? 我在绘制语法时遇到麻烦,而且NaT值也使得很难绘制每列的图形吗? New to Python so would appreciate any help, thank you. Python的新手,非常感谢您的帮助,谢谢。

It might be easier to transpose the dataframe such that the datetimes are the indices, but I am not too sure how to do that either. 将数据帧转置为日期时间为索引可能会更容易,但是我也不太确定该怎么做。

You can loop through every column of your dataframe, drop the missing values with dropna() , reset the index (and add 1 = number of meetings) and plot it... like you correctly said with values and index reversed: 您可以遍历数据帧的每一列,使用dropna()删除缺少的值,重置索引(并添加1 =会议数量)并对其进行绘图...就像您正确地说过的那样,值和索引颠倒了:

fig = plt.figure()
for c in df:
    person = df[c].dropna().reset_index(drop=True)
    person.index = person.index+1
    plt.plot(person.values, person.index)
plt.xlabel('Date')
plt.ylabel('Number of Meetings')

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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