0

I have a temporary dataframe temp (as shown below) sliced from a larger dataframe.

enter image description here

I appreciate it if help me to assign the item_price value of each row to a related column associated with model as shown below:

enter image description here

Note: original and larger dataframe contains brands, prices and models which some of the rows have a similar brand name with different model and price, so I slice those similar records into temp dataframe and try to assign price to related columns associated with model for each record.

Thanks in advance!

2 Answers2

2

If I were you I would delete the columns 'Sedan', 'Sport' and 'SUV' and use pivot
In your case you would want to do the following:
Create a new Dataframe called df1 like so:

df1 = df.pivot(index='brand', columns='model', values='item_price')

And then join your original DataFrame df1 with df1.

df = df.join(df1, on='brand')

This will give you the result you are looking for.

Ofek Glick
  • 999
  • 2
  • 9
  • 20
0

You can create a method that returns the value based on a condition like this:

I'm using df as the name of the dataframe, you can rename to temp.

def set_item_price(model):
  if model == "Sedan":
    return 78.00

  return 0

df["item_price"] = [
  set_item_price(a) for a in df['model']
]