1

I have a sample of my dataframe as below:

Product Category    Books, VHS videotapes, Software, Video Games
Per Shipment                                               $4.99
Per Item                                                   $2.49
Per Item Remarks                           Heavy media surcharge

This should the data for one row in my dataframe, each row has four columns.

What I want to do is to split the Product Category item into several components by ,, and then this row will be expanded into four, data for the remaining three columns will be the same.

I have written a explicit loop like below:

for index, row in raw_table.iterrows():
   temp_product = row['Product Category'].split(',')
   temp_product = [re.sub('^ ','',x) for x in temp_product]
   row['Product Category'] = temp_product

I succeed in spliting the Product Category column for each row into multiple components, but I dunno how to "expand" each single row, the line row['Product Category'] = temp_product is not working.

I missed the data.frame function in R, which I will do in this way in R:

row.mod <- data.frame('product_category'=temp_product,
                      'per_shipment'=row['per_shipment'],
                      'per_item'=row['per_item'],
                      'per_item_remarks'=row['per_item_remarks'])

Can anyone enlighten me on how to do this in python? Thanks!

lokheart
  • 23,743
  • 39
  • 98
  • 169

0 Answers0