Jul-20-2018, 06:04 AM
Hi All,
I have a csv file containing product details. From which I am extracting required informations.
There is one column in the csv-Options and it contains values like- [], [['Newborn (2.5-6kg)', 'Big Baby (6-9kg)']].
I have written an unnesting function and applying on Options so that values looks like= Newborn (2.5-6kg),Big Baby (6-9kg).
But th eproblem with this function is that it is removing the [] from the Options and so all rows containing Options as [] are removed and so I am loosing the products.
I need to accept empty Options values also so that there is no loss of product while unnesting.
PFB my unnesting function-
I have a csv file containing product details. From which I am extracting required informations.
There is one column in the csv-Options and it contains values like- [], [['Newborn (2.5-6kg)', 'Big Baby (6-9kg)']].
I have written an unnesting function and applying on Options so that values looks like= Newborn (2.5-6kg),Big Baby (6-9kg).
But th eproblem with this function is that it is removing the [] from the Options and so all rows containing Options as [] are removed and so I am loosing the products.
I need to accept empty Options values also so that there is no loss of product while unnesting.
PFB my unnesting function-
def unnest(df, col, col2,reset_index=False): col_flat = pd.DataFrame([[i, x] for i, y in df[col].apply(list).iteritems() for x in y ], columns=['I', col] ) col_flat = col_flat.set_index('I') df = df.drop(col, 1) df = df.merge(col_flat, left_index=True, right_index=True) if reset_index: df = df.reset_index(drop=True) merchant_product_code = (df['Product code'] + '-' + df[col]) df['item_group_id'] = df['Product code'] df['Product code'] = merchant_product_code return dfI am applying above function as below-
full_df = unnest(full_df,'Options','product code')I have attached the full_df csv, please have a look and correct me what I am missing here.