Python Forum
Getting duplicate column values but same row values from a csv
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Getting duplicate column values but same row values from a csv
#1
I am SO STUCK and its driving me mad. I have a CSV like so:

Quote:date, order_id, sku, transaction_type, payment_type, payment_detail, amount, quantity
20 Nov 2019 026-8286010-8145911 P6-VZ65-2FQT Order Payment Amazon fees Commission £-5.82
20 Nov 2019 026-8286010-8145911 P6-VZ65-2FQT Order Payment Amazon fees FBA fulfilment fee per unit £-7.73
20 Nov 2019 026-8286010-8145911 P6-VZ65-2FQT Order Payment Product charges £40.42 1
20 Nov 2019 026-8286010-8145911 P6-VZ65-2FQT Order Payment Promo rebates £0.00 1
20 Nov 2019 026-8286010-8145911 P6-VZ65-2FQT Order Payment Other Product Tax £8.08
20 Nov 2019 026-8286010-8145911 P6-VZ65-2FQT Order Payment Amazon fees Commission £-5.82
20 Nov 2019 026-8286010-8145911 P6-VZ65-2FQT Order Payment Amazon fees FBA fulfilment fee per unit £-7.73
20 Nov 2019 026-8286010-8145911 P6-VZ65-2FQT Order Payment Product charges £40.42 1
20 Nov 2019 026-8286010-8145911 P6-VZ65-2FQT Order Payment Promo rebates £0.00 1
20 Nov 2019 026-8286010-8145911 P6-VZ65-2FQT Order Payment Other Product Tax £8.08
19 Nov 2019 206-3581742-6918727 N4-AMGB-L9HS Order Payment Amazon fees Commission £-8.58
19 Nov 2019 206-3581742-6918727 N4-AMGB-L9HS Order Payment Amazon fees FBA fulfilment fee per unit £-5.97
19 Nov 2019 206-3581742-6918727 N4-AMGB-L9HS Order Payment Amazon fees Shipping chargeback £-0.93
19 Nov 2019 206-3581742-6918727 N4-AMGB-L9HS Order Payment Product charges £59.58 1
19 Nov 2019 206-3581742-6918727 N4-AMGB-L9HS Order Payment Other Product Tax £11.92
19 Nov 2019 206-3581742-6918727 N4-AMGB-L9HS Order Payment Other Shipping tax £0.19
19 Nov 2019 206-3581742-6918727 N4-AMGB-L9HS Order Payment Other Shipping £0.93
1

import csv

order_ids = []
info = []

with open('report6.csv', mode='r', encoding='utf-8') as infile:
    product_charges = ""
    product_tax = ""
    reader = csv.reader(infile)
    data = [rows for rows in reader]
    skipped = data[4:]
    for i in skipped:
        order_id = i[1]
        if order_id in order_ids:
            sku = i[2]
            transaction_type = i[3]
            payment_type = i[4]
            if payment_type == "Product charges":
                product_charges = i[6]
                product_charges = product_charges.replace("£", "")
            payment_detail = i[5]
            if payment_detail == "Product Tax":
                product_tax = i[6]
                product_tax = product_tax.replace("£", "")

            format = [order_id, [product_charges, product_tax]]
            info.append(format)
            continue
        else:
            order_ids.append(order_id)
            sku = i[2]
            transaction_type = i[3]
            payment_type = i[4]
            if payment_type == "Product charges":
                product_charges = i[6]
                product_charges = product_charges.replace("£", "")
            payment_detail = i[5]
            if payment_detail == "Product Tax":
                product_tax = i[6]
                product_tax = product_tax.replace("£", "")

            format = [order_id, [product_charges, product_tax]]
            info.append(format)
            continue

for a in range(0, 1, len(order_ids)):
    for b in info:
        while order_ids[a] == b[0]:
            a +=1
And all the data per order_id is on a separate row, and I am trying to get it all on the same row - in particular, the product_charges and product_tax.
For some orders, there is duplicate amounts of product_charges and product_taxes - and I just dont know how to handle these...

I want my output to be like:
Output:
026-8286010-8145911, 80.82, 16.16 206-3581742-6918727, 59.58, 11.92
Any help would be immensely appreciated!
Reply
#2
This reads every row at once, which is OK if that's what you intended

also be aware that your skipped is skipping the entire the 5th row and beyond

example:
>>> zz = [
...     ['a', 'b'], ['b', 'c'], ['d', 'e'], 
...     ['f', 'g'], ['h', 'i'], ['j', 'k']
... ]
>>> 
>>> skipped = zz[:4]
>>> skipped
[['a', 'b'], ['b', 'c'], ['d', 'e'], ['f', 'g']]
>>>
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Printing out incidence values for Class Object SquderDragon 3 258 Apr-01-2024, 07:52 AM
Last Post: SquderDragon
  Get an average of the unique values of a column with group by condition and assign it klllmmm 0 252 Feb-17-2024, 05:53 PM
Last Post: klllmmm
  Too much values to unpack actualpy 3 454 Feb-11-2024, 05:38 PM
Last Post: deanhystad
  Converting column of values into muliple columns of counts highland44 0 249 Feb-01-2024, 12:48 AM
Last Post: highland44
  __init__() got multiple values for argument 'schema' dawid294 4 2,211 Jan-03-2024, 09:42 AM
Last Post: buran
  How to access values returned from inquirer cspower 6 777 Dec-26-2023, 09:34 PM
Last Post: cspower
  partial functions before knowing the values mikisDeWitte 4 589 Dec-24-2023, 10:00 AM
Last Post: perfringo
  need to compare 2 values in a nested dictionary jss 2 846 Nov-30-2023, 03:17 PM
Last Post: Pedroski55
  Copying the order of another list with identical values gohanhango 7 1,133 Nov-29-2023, 09:17 PM
Last Post: Pedroski55
  Can I retrieve 2 values in a for ? Kakalok 3 520 Nov-06-2023, 10:35 PM
Last Post: menator01

Forum Jump:

User Panel Messages

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