python - 查找最常见的元素和排序数据

python - 查找最常见的元素和排序数据,第1张

我有一个大型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。

输出将像这张表一样布局(显然有不同的输入) python - 查找最常见的元素和排序数据,enter image description here,第2张

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

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

最佳答案:

2 个答案:

答案 0 :(得分:0)

怎么样

按来电者.sort('callers', ascending = False)

排序

将来电者设为索引.set_index('callers')

选择您感兴趣的其他列

转置.T

答案 1 :(得分:0)

在SQL中执行时,这可能是最干净/最快的,只要你感觉舒适。假设您已经创建了包含数据的表格,此查询将为您提供前5位客户:

create temporary table top_5 as 
    select * from (select CallerName,max(count) as num_calls 
    from complaints group by CallerName) order by num_calls desc limit 5;


select CallerName, num_calls,
(select EmpName from complaints where CallerName=c.CallerName order by count desc limit 1) as top_employee, 
(select CallDesc from complaints where CallerName=c.CallerName order by Received_Data_and_Time desc limit 1) as recent_desc, 
(select Gen_code from complaints where CallerName=c.CallerName order by Received_Data_and_Time desc limit 1) as recent_gencode 
from complaints c join top_5 on top_5.CallerName=c.CallerName;

当然,您需要确保在输入数据库时​​以正确的方式排序日期,或者您可以使用某种类型的字符串日期解析功能来使用您想要使用的数据库。 Sqlite可能是我的偏好,并且非常适合大多数报告式的任务,只要你在过去的20年里没有向Comcast报告投诉。

本文经用户投稿或网站收集转载,如有侵权请联系本站。

发表评论

0条回复