按公共日期对数组数据进行排序 [英] Sorting array data by common date

查看:56
本文介绍了按公共日期对数组数据进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个.csv文件,其中包含许多行和3列:日期,代表和销售.我想使用Python生成一个新数组,该数组按日期对数据进行分组,并在给定日期按销售对销售代表进行排序.例如,我的输入数据如下:

I have a .csv file with many rows and 3 columns: Date, Rep, and Sales. I would like to use Python to generate a new array that groups the data by Date and, for the given date, sorts the Reps by Sales. As an example, my input data looks like this:

salesData = [[201703,'Bob',3000], [201703,'Sarah',6000], [201703,'Jim',9000], 
    [201704,'Bob',8000], [201704,'Sarah',7000], [201704,'Jim',12000], 
    [201705,'Bob',15000], [201705,'Sarah',14000], [201705,'Jim',8000],
    [201706,'Bob',10000], [201706,'Sarah',18000]]

我想要的输出看起来像这样:

My desired output would look like this:

sortedData = [[201703,'Jim', 'Sarah', 'Bob'], [201704,'Jim', 'Bob', 
    'Sarah'], [201705,'Bob', 'Sarah', 'Jim'], [201706, 'Sarah', 'Bob']]

我是Python的新手,但我已经搜索了很多解决方案,但都没有成功.我的大部分搜索结果使我相信,使用pandas(我没有使用过)或numpy(我已经使用过)可能有一种简单的方法.

I am new to Python, but I have searched quite a bit for a solution with no success. Most of my search results lead me to believe there may be an easy way to do this using pandas (which I have not used) or numpy (which I have used).

任何建议将不胜感激.我正在使用Python 3.6.

Any suggestions would be greatly appreciated. I am using Python 3.6.

推荐答案

使用熊猫!

import pandas as pd

salesData = [[201703, 'Bob', 3000], [201703, 'Sarah', 6000], [201703, 'Jim', 9000],
             [201704, 'Bob', 8000], [201704, 'Sarah', 7000], [201704, 'Jim', 12000],
             [201705, 'Bob', 15000], [201705, 'Sarah', 14000], [201705, 'Jim', 8000],
             [201706, 'Bob', 10000], [201706, 'Sarah', 18000]]

sales_df = pd.DataFrame(salesData)
result = []
for name, group in sales_df.groupby(0):
    sorted_df = group.sort_values(2, ascending=False)
    result.append([name] + list(sorted_df[1]))
print(result)

这篇关于按公共日期对数组数据进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆