Python 中包含相关信息的前五名表

Posted

技术标签:

【中文标题】Python 中包含相关信息的前五名表【英文标题】:Table of Top Five with Pertinent Information in Python 【发布时间】:2015-08-29 01:27:42 【问题描述】:

我有一个大的 csv 文件,我想将其转换为包含相关信息的表格。我会选择前五名来电者,然后在下方添加一些数据,例如最后一次来电说明,以及接到该客户来电次数最多的人。

是这样的:

ComplaintNumber;CompanyName;CallerName;EmpName;CallStatus;Gen_Code;Detail Code;OtherDetail;CallDesc;QualityIssue;HighPriority;Received Data and Time; Call Type;count;Unnamed: 14;Unnamed: 15;Unnamed: 16;Unnamed: 17;Unnamed: 18;Unnamed: 19;Unnamed: 20;Unnamed: 21;Unnamed: 22;Unnamed: 23   
5651;Company 1;joe;Rob;Closed;Maintenance;Code 1;;Sentence that is relatively long but not too long;No;1-No;6/11/2015 15:00;Type 1;8;;;;;;;;;;  
6642;Company 1;joe;Rob;Closed;Support;Code 2;;another sentence that is relatively long but not too long;No;1-No;6/12/2015 15:00;Type 2;8;;;;;;;;;;  
6893;Company 1;joe;Rob;Closed;Support;Code 2;;description and stuff;No;1-No;6/13/2015 15:00;Type 3;8;;;;;;;;;;  
4535;Company 1;joe;Gwen;Closed;Maintenance;Code 4;;So and so called and said such and such;No;1-No;6/14/2015 15:00;Type 2;8;;;;;;;;;;   
8655;Company 1;joe;Gwen;Closed;Support;Code 56;;Somebody needs a nap     apparently;No;1-No;6/15/2015 15:00;Type 1;8;;;;;;;;;;
8656;Company 1;joe;Wendy;Closed;Computer;Code 12;;Yup    just typing random notes here;No;1-No;6/12/2015 15:00;Type 1;8;;;;;;;;;;
8857;Company 1;joe;Sarah;Closed;Computer;Code 3;;Yup     just typing random notes here;No;1-No;6/17/2015 15:00;Type 4;8;;;;;;;;;;
3348;Company 1;joe;John;Closed;Computer;Code;Red;Yup     just typing random notes here;No;1-No;6/18/2015 15:00;Type 4;8;;;;;;;;;;
6679;Company 2;Belinda;John;Closed;Maintenance;Code 4;;Yup   just typing random notes here;No;1-No;6/4/2015 19:00;Type 4;7;;;;;;;;;;
5510;Company 2;Belinda;Bob;Closed;Support;Code 1;;Yup    just typing random notes here;No;;6/20/2015 15:00;Type 4;7;;;;;;;;;;
7711;Company 2;Belinda;Bob;Closed;Support;Code 1;;Yup    just typing random notes here;No;1-No;6/21/2015 12:00;Type 4;7;;;;;;;;;;
6212;Company 2;Belinda;Bob;Closed;Support;Code 4;;Yup    just typing random notes here;No;1-No;6/22/2015 15:00;Type 2;7;;;;;;;;;;
4413;Company 2;Belinda;Bob;Closed;Support;Code 34;;Yup   just typing random notes here;No;1-No;6/23/2015 5:00;Type 2;7;;;;;;;;;;
1444;Company 2;Belinda;Bob;Closed;Support;Code 2;Blue;Yup    just typing random notes here;No;1-No;6/23/2015 15:00;Type 2;7;;;;;;;;;;
5515;Company 2;Rodger;Yolanda;Closed;Maintenance;Code 1;;Yup     just typing random notes here;No;1-No;6/25/2015 15:00;Type 2;7;;;;;;;;;;
1756;Company 3;Janet;Yolanda;Closed;Maintenance;Code 2;;Yup  just typing random notes here;No;1-No;6/26/2015 15:00;Type 2;5;;;;;;;;;;
1557;Company 3;Janet;Yolanda;Closed;Computer;Code 4 ;;Yup    just typing random notes here;No;1-No;6/27/2015 15:00;Type 2;5;;;;;;;;;;
1238;Company 3;Janet;Yolanda;Closed;Computer;Code 45;Purple;Yup  just typing random notes here;No;1-No;6/28/2015 15:00;Type 1;5;;;;;;;;;;
1729;Company 3;Richard;Steve;Closed;Computer;Code 2 ;;Yup    just typing random notes here;No;1-No;6/29/2015 15:00;Type 1;5;;;;;;;;;;
2340;Company 3;Richard;Fred;Closed;Support;Code 4;Yellow;Yup     just typing random notes here;No;1-No;6/30/2015 15:00;Type 3;5;;;;;;;;;;
2131;Company 4;Pamela;Rob;Closed;Maintenance;Code 5;;Yup     just typing random notes here;No;1-No;7/1/2015 15:00;Type 3;3;;;;;;;;;;
2662;Company 4;Pamela;Rob;Closed;Maintenance;Code 6;;Yup     just typing random notes here;No;1-No;7/2/2015 15:00;Type 3;3;;;;;;;;;;
2833;Company 4;Pamela;Rob;Closed;Maintenance;Code 7;;Yup     just typing random notes here;No;1-No;7/3/2015 15:00;Type 3;3;;;;;;;;;;
2564;Company 5;Stan;Steve;Closed;Computer;Code 8;;Yup    just typing random notes here;No;1-No;7/4/2015 15:00;Type 3;1;;;;;;;;;;
2225;Company 6;Lee;Steve;Closed;Computer;Code 9;;Yup     just typing random notes here;No;1-No;7/5/2015 15:00;Type 4;1;;;;;;;;;;
1326;Company 7;Jackie;Steven;Closed;Support;Code 10;;Yup     just typing random notes here;No;1-No;7/6/2015 15:00;Type 1;1;;;;;;;;;;
7227;Company 8;Jake;Rob;Closed;Support;Code 11;;Yup  just typing random notes here;No;1-No;7/7/2015 15:00;Type 2;1;;;;;;;;;;
4228;Company 9 ;Steve;Wendy;Closed;Computer;Code 12;;Yup     just typing random notes here;No;1-No;7/8/2015 15:00;Type 3;1;;;;;;;;;;

我想制作一个表格,其中前五位来电者作为标题,在 EmpName(从该客户接听最多电话的人)、Gen_code 和最近的一个句子(电话也有日期和收到时间列)CallDesc。

输出将像这张表一样布局(显然输入不同)

即使只是有关如何解决此问题的指导,我也将不胜感激。伪代码很好。如果有一种快速、简单的方法,那就更好了!

到目前为止,我已经对列表进行了排序,以便出现频率最高的客户位于列表顶部。但是从那里我不知道如何获取最新的通话描述,甚至不知道如何只选择前五名。

【问题讨论】:

【参考方案1】:

将表格读入 pandas DataFrame:

import pandas as pd
filename = 'my_file.csv'
df = pd.read_csv(filename, sep=';')

创建数据透视表:雇主姓名与来电者姓名。

pivot = df.pivot_table(index='CallerName', columns='EmpName', values='ComplaintNumber', aggfunc='count')
# clean the table from NaNs (not necessary, but more beautiful):
pivot.fillna(0, inplace=True)

创建一个新列 sum 并将每个调用者的总和保存到其中:

pivot['sum'] = pivot.sum(axis=1)

使用求和列降序对表格进行排序:

pivot.sort('sum', ascending=False, inplace=True)

打印此表的前 5 个结果

print(pivot[:5])

我希望这是你想要的!

编辑:您现在可以按呼叫者对原始数据框进行分组,只选择前 5 个并显示所需的所有信息。

EDIT2:“pivot[:5].transpose()”将使您非常接近您建议的最终表格形式,其中前五个调用者是表格“标题”

【讨论】:

这与我想要的很接近,但并不完全!这提供了一个很好的表格,其中包含最多的呼叫者和列,其中包含 Emp Names 以及每个员工接到多少电话......但我正在寻找它来选择为呼叫者接听最多电话的一名员工,并在其下方,该客户出现次数最多的 Gen_Code。

以上是关于Python 中包含相关信息的前五名表的主要内容,如果未能解决你的问题,请参考以下文章

MS ACCESS TransferSpreadsheet VBA 在导入数据中包含额外信息

在代码中访问.webtestResult文件中包含的信息

不能在 Visual Studio 中包含 Python.h

《信息与编码》考试复习笔记----前五章题目补充

Python:在csv表中自动获得销量前五的商品信息

Python:在csv表中自动获得销量前五的商品信息