Sorting array data by common date

I have a CSV file with many rows and three columns: Date, Rep and Sales. I would like to use Python to create a new array that groups the data by date and sorts Reps by Sales on a given date. As an example, my input 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']]

      

I'm new to Python, but I've searched quite a bit for a solution with no success. Most of my search results make me think there might be an easy way to do this using pandas (which I haven't used) or numpy (which I have used).

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

+3


source to share


2 answers


Use Pandas!



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)

      

+2


source


Without pandas, you can try one line of answer:

sortedData = [[i]+[item[1] for item in salesData if item[0]==i] for i in sorted(set([item[0] for item in salesData]))]

      




EDIT:
You can do this to order each internal sales list:
sortedData = [[i]+[item[1] for item in sorted(salesData, key=lambda x: -x[2]) if item[0]==i] for i in sorted(set([item[0] for item in salesData]))]

      

Note that the part sorted(salesData, key=lambda x: -x[2])

does the ordering

0


source







All Articles