Python:使用Pandas Pivot_table

时间:2018-09-08 20:50:43

标签: python pandas matplotlib

我有一个CSV充当日志文件/数据库。它的结构如下:

User1,Client3,Ops/Admin,19:33:46,19:33:57,178.054662,Notes,05/09/2018
User1,Client2,Ops/Admin,20:33:46,20:33:57,449.272576,Notes,05/09/2018
User1,Client3,Support Management,21:33:46,21:33:57,1073.425258,Notes,05/09/2018
User1,Client2,Support Management,22:33:46,22:33:57,290.640219,Notes,05/09/2018
User1,Client3,Ops/Admin,23:33:46,23:33:57,402.223927,Notes,05/09/2018
User1,Client1,Support Management,00:33:46,00:33:57,761.266062,Notes,05/09/2018
User1,Client1,Business Developement,01:33:46,01:33:57,1623.303656,Notes,05/09/2018

我想使用matplotlib.pyplot制作折线图,该折线求和每个日期的每个客户的col [5]总数。 IE在同一张图表上显示另一条线,其中x轴是日期(col [7]),y轴是该日期值的总和(col [5])。

我正在使用熊猫,这是我的出发点:

import matplotlib.pyplot as plt
import pandas as pd
import datetime, csv

csv_file = pd.read_csv("file.csv",
                           names = ['USER',
                                   'CLIENT',
                                   'TYPE',
                                   'START',
                                   'END',
                                   'DURATION',
                                   'NOTES',
                                   'DATE'])

然后我尝试将表制作为数据透视表:

date_pivot = csv_file.pivot_table('DURATION', index='CLIENT', columns='DATE')

或使用.groupby()函数:

dategroup = csv_file.groupby(['CLIENT','DATE'], as_index = False).sum()

这两个表似乎都生成了一个看起来足以使用的表。

问题是,由于不乏尝试(...),我无法弄清楚如何在DATE上在matplotlib.pyplot.plot()上绘制CLIENT的求和值。 >

到目前为止我唯一的想法:

  • 我是否应该以某种方式重新排列表格,以使客户端沿Y轴(/ index)运行并沿x轴(/ header)进行约会?
  • 是否需要使用.loc()的某种形式的def迭代函数?

在此方面的任何帮助将不胜感激,在此先感谢您!

2 个答案:

答案 0 :(得分:0)

这会再次根据客户将客户和日期的求和结果分成几组,并为每个客户绘制一条单独的线:

import matplotlib.pyplot as plt
import pandas as pd

df['DATE'] = pd.to_datetime(df['DATE'])
aggregated = df.groupby(['CLIENT', 'DATE']).sum().reset_index()

fig, ax = plt.subplots()

for key, group in aggregated.groupby(['CLIENT']):
    ax = group.plot(ax=ax, kind='line', x='DATE', y='DURATION', label=key)

plt.show()

这是基于this答案中的分组绘图技巧。

答案 1 :(得分:0)

<?php

try {
  // File Route.
  $fileRoute = "/uploads/";

  $fieldname = "file";

  // Get filename.
  $filename = explode(".", $_FILES[$fieldname]["name"]);

  // Validate uploaded files.
  // Do not use $_FILES["file"]["type"] as it can be easily forged.
  $finfo = finfo_open(FILEINFO_MIME_TYPE);

  // Get temp file name.
  $tmpName = $_FILES[$fieldname]["tmp_name"];

  // Get mime type.
  $mimeType = finfo_file($finfo, $tmpName);

  // Get extension. You must include fileinfo PHP extension.
  $extension = end($filename);

  // Allowed extensions.
  $allowedExts = array("gif", "jpeg", "jpg", "png", "svg", "blob");

  // Allowed mime types.
  $allowedMimeTypes = array("image/gif", "image/jpeg", "image/pjpeg", "image/x-png", "image/png", "image/svg+xml");

  // Validate image.
  if (!in_array(strtolower($mimeType), $allowedMimeTypes) || !in_array(strtolower($extension), $allowedExts)) {
    throw new \Exception("File does not meet the validation.");
  }

  // Generate new random name.
  $name = sha1(microtime()) . "." . $extension;
  $fullNamePath = dirname(__FILE__) . $fileRoute . $name;

  // Check server protocol and load resources accordingly.
  if (isset($_SERVER["HTTPS"]) && $_SERVER["HTTPS"] != "off") {
    $protocol = "https://";
  } else {
    $protocol = "http://";
  }

  // Save file in the uploads folder.
  move_uploaded_file($tmpName, $fullNamePath);

  // Generate response.
  $response = new \StdClass;
  $response->link = $protocol.$_SERVER["HTTP_HOST"].dirname($_SERVER["PHP_SELF"]).$fileRoute . $name;

  // Send response.
  echo stripslashes(json_encode($response));

} catch (Exception $e) {
   // Send error response.
   echo $e->getMessage();
   http_response_code(404);
}
?>
  • x轴/索引/日期
  • 多个y轴/列/客户
  • 聚合函数= sum