Python Forum
Insert into sublist if sublist is not having same no of records.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Insert into sublist if sublist is not having same no of records.
#10
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
Reply


Messages In This Thread
RE: Insert into sublist if sublist is not having same no of records. - by michalmonday - May-28-2019, 11:30 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Use one list as search key for another list with sublist of list jc4d 4 2,221 Jan-11-2022, 12:10 PM
Last Post: jc4d
Question Sublist/ Subarray into string Python SantiagoPB 2 2,171 Apr-23-2021, 07:03 PM
Last Post: SantiagoPB
  List of objects with sublist medatib531 4 2,372 Mar-01-2020, 06:16 PM
Last Post: buran
  Split List and Sublist from Pyodbc parthi1705 1 2,259 May-05-2019, 10:44 AM
Last Post: Larz60+
  merging sublist into single list in python abhishek8singhai 8 9,609 Mar-22-2019, 11:46 PM
Last Post: micseydel
  Can I use a sublist as an argument in python? MartinBerlin 3 4,069 Aug-25-2018, 10:46 AM
Last Post: buran
  Insert using psycopg giving syntax error near "INSERT INTO" olgethorpe 4 15,737 Jul-21-2017, 07:39 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020