熊猫-分组后绘制图形的问题

时间:2020-09-16 14:09:34

标签: python pandas matplotlib

我在sqlite3中有一个“出勤”表,我正在使用熊猫将其作为数据框导入。数据框看起来像这样,

      id    name       date                time
0  12345  Pankaj 2020-09-12 1900-01-01 23:17:49
1  12345  Pankaj 2020-09-12 1900-01-01 23:20:28
2  12345  Pankaj 2020-09-13 1900-01-01 13:36:01

一个“ id”的人可以出现多次,这相当于一个人一天多次出入门,我们正在记录每个过渡。

我希望找到倒数第二次进来的时间,以找出一个人在公司工作的时间。

由于我们一次只需要一个人的数据,所以我首先要过滤一个人的数据,就像这样。

df = df.loc[df['id']== id]

这留给我一个特定人的所有条目。

现在,对于最后输入时间和第一次输入时间的差,我正在这样计算,

df_gp = df.groupby('date')['time']
difference = df_gp.max() - df_gp.min()

现在,“差异”作为熊猫系列出现。

date
2020-09-12   00:02:39
2020-09-13   00:00:00

当我尝试使用类型为kind ='line'的pandas.series.plot()方法绘制图形时,

difference.plot(kind = 'line')

我完全看不到图形。我看不到任何此类错误,只是根本不显示任何内容。

我打印时,

print(difference.plot(kind = 'line'))

它将在终端上打印此

AxesSubplot(0.125,0.2;0.775x0.68)

因此,我认为图必须被破坏并过快退出函数,这与time.sleep()有关,但事实并非如此,我尝试了很多事情,但根本没有显示出来。

我需要帮助-

  1. 我不;当我想在特定的一天中有不同的时间时,知道这是否是绘制图表的正确方法。请建议您是否有任何优雅的方法可以做到这一点。
  2. 根本不显示的原因是什么?

完整代码

def main():
    emp_id = "12345"
    db = os.path.join(constants.BASE_DIR.format("db"),"db_all.db")
    with closing(sqlite3.connect(db)) as conn:
        df = pd.read_sql_query("select * from attendance where id = {} order by date ASC".format(emp_id), conn, parse_dates={'date':'%Y-%m-%d',
            'time':'%H:%M:%S'})

    print(df.head())
    #df = df.loc[df['id']== id]
    is_empty = df.empty
    if is_empty:
        messagebox.showerror("Error","There are not enough records of employee")
        return
    
    # Add the latest row
    emp_name = df.loc[(df['id'] == id).idxmax(),'name']
    # dt_time = datetime.datetime.now().replace(microsecond=0)
    # _date, _time = dt_time.date(),dt_time.time()


    # print(type(_date))
    # print(type(_time))
    # df.loc[-1] = [emp_id,emp_name,_date,_time]

    # df.index += 1
    # df = df.sort_index()

    # print(df.dtypes)
    df_gp = df.groupby('date')['time']
    print("Here")
    difference = df_gp.max() - df_gp.min()
    print(difference)
    print(difference.plot(kind = 'line'))



if __name__ == '__main__':
    main()

-谢谢

0 个答案:

没有答案