Click here to Skip to main content
15,565,568 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In my dataset, I have three columns: product, marketplace, and product type. For products with a different product type, I must replace the product_type value with the value in the following order: marketplace = 200, 300, 400 and 500. The input file appears to be as follows:
What I have tried:

Because we are unable to take the information for marketplace == 200, we are switching to marketplace == 300 and replacing the value for all of the sets that I have, for product == 3. This is the code that I've used:
<pre lang="python">
mp_correspondence = {200:1, 
                     300:2,
                     400:3,
                     500:4, 
                   }
df['ranking'] = df['marketplace'].map(mp_correspondence)
number_list = set(df['product '])


for i in product_list:
    df_product_frame = df[df['product '] == i].copy()
    nr_rows = df_product_frame['product'].count()
    if nr_rows > 1:
        product_type_count = set(df_product_frame['product_type'])
        print(product_type_count )
        preced_value = set(df['ranking'])
        print(preced_value)
        if product_type_count != 1:
           res = next(iter(preced_value))
           print("The first key of dictionary is:" + str(res))      
    else:
        print('Lesser than 1')

But first and foremost, the precedence ranking column does not display the correct values associated with the marketplace's product type. Is there any way you could assist in resolving the error and obtaining a method to replace the values?
Posted
Updated 15-Mar-22 2:23am
v4

1 solution

If you copy the dataset into a simple list you can do it with the following:
Python
products = [
    [ 1, 200, 'X' ],
    [ 2, 300, 'A' ],
    [ 2, 400, 'A' ],
    [ 2, 200, 'A' ],
    [ 3, 500, 'A' ],
    [ 3, 400, 'A' ],
    [ 3, 300, 'B' ],
]
for i in range(len(products)):
    if products[i][0] == 3:
        products[i][2] = 'B'
    
print(products)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900