May-28-2019, 11:30 AM
(This post was last modified: May-28-2019, 12:15 PM by michalmonday.)
I added pandas way too, trying to make it a little more concise, it would be cool if some pandas wizard would post an optimal solution just to check how good it can possibly look like
def Decimal(x): return x results = [ (Decimal('28000.0000'), 2010, 272), (Decimal('308000.0000'), 2011, 272), (Decimal('582000.0000'), 2012, 272), (Decimal('387000.0000'), 2013, 272), (Decimal('367000.0000'), 2010, 281), (Decimal('3037000.0000'), 2011, 281), (Decimal('4822000.0000'), 2012, 281), (Decimal('2936000.0000'), 2013, 281), (Decimal('637000.0000'), 2010, 282), (Decimal('3440000.0000'), 2011, 282), (Decimal('478000.0000'), 2010, 296), (Decimal('1209000.0000'), 2012, 296) ] #Desired output years = sorted(set([x[1] for x in results])) employees = sorted(set([x[2] for x in results])) sales = [] for emp in employees: # get all the results for that particular employee emp_results = [x for x in results if x[2] == emp] # init/fill emp_sales with as many 0's as there are years emp_sales = [0] * len(years) for year_index, year in enumerate(years): for result_year, result_sales_amount in [(x[1], x[0]) for x in emp_results]: if year == result_year: emp_sales[year_index] = result_sales_amount sales.append(emp_sales) print('years:\n', years, '\n') print('employees:\n', employees, '\n') print('sales:\n', sales, '\n')
Output:years:
[2010, 2011, 2012, 2013]
employees:
[272, 281, 282, 296]
sales:
[['28000.0000', '308000.0000', '582000.0000', '387000.0000'], ['367000.0000', '3037000.0000', '4822000.0000', '2936000.0000'], ['637000.0000', '3440000.0000', 0, 0], ['478000.0000', 0, '1209000.0000', 0]]
import pandas as pd def Decimal(x): return x results = [ (Decimal('28000.0000'), 2010, 272), (Decimal('308000.0000'), 2011, 272), (Decimal('582000.0000'), 2012, 272), (Decimal('387000.0000'), 2013, 272), (Decimal('367000.0000'), 2010, 281), (Decimal('3037000.0000'), 2011, 281), (Decimal('4822000.0000'), 2012, 281), (Decimal('2936000.0000'), 2013, 281), (Decimal('637000.0000'), 2010, 282), (Decimal('3440000.0000'), 2011, 282), (Decimal('478000.0000'), 2010, 296), (Decimal('1209000.0000'), 2012, 296) ] pd.set_option('display.float_format', '{:.2f}'.format) df = pd.DataFrame(results, columns = ['sales' , 'year', 'employee']) years = df['year'].unique() employees = df['employee'].unique() new_df = pd.DataFrame(index = employees, columns = years) for emp in employees: emp_df = df.loc[df['employee'] == emp] new_df.loc[emp, : ] = [float(emp_df.loc[emp_df['year']==year]['sales']) if year in list(emp_df['year']) else 0 for year in years] print(new_df)
Output: 2010 2011 2012 2013
272 28000.00 308000.00 582000.00 387000.00
281 367000.00 3037000.00 4822000.00 2936000.00
282 637000.00 3440000.00 0 0
296 478000.00 0 1209000.00 0